Skip to main content
Arduino

Return to Answer

edit 2
Source Link

Edit[Part of] Edit 1b: In the larger scheme of things perhaps there's no need to create and save a formatted string like that. It is not useful for computing – the float values are better for that. It is not needed for communication – another streaming write can be done.


Edit 2 [replaces most of 1b]: Streaming.h as-is doesn't doesn't build any strings, but just delivers the text of its <<<<-arguments character-by-character to a stream.

A However, a StringStream class at github PString class at arduiniana can build strings from stream inputs, so foras in the following example if sst is a StringStream object,code.

sst#include <<<Streaming.h>
#include "After<PString.h>
char "buffer[28];
PString <<pstr(buffer, millissizeof(buffer));
void <<setup() "{
 milliseconds, we'reSerial.begin(115200);
 at "pstr.begin(); << _FLOAT // Empty the buffer
}
void loop(latitude) {
 int first=50,6 second=100;
 pstr.begin(); // Empty the buffer
 pstr << ",'X' << first << 'Y' << second << 'T';
  pstr += " hihi ";
 pstr << _FLOATmillis(longitude,6) << endl;
 Serial << pstr;
 delay(1000);
}

willHere's what some of its output to Serial looks like:

X50Y100T hihi 263198
X50Y100T hihi 264199
X50Y100T hihi 265199
X50Y100T hihi 266201
X50Y100T hihi 267201
X50Y100T hihi 268201

What the sample of code does is first initialize a string buffer, then build part of a character string internal tousing streaming operator sst<<, which can be read out character by characterthen part of it using StringStream'sconcatenation, then some more with read()<< method, or if you modify the class, can be read out however you like.

Inthen print the larger scheme of things perhaps there's no need to create and save a formatted string like that. It is not useful for computing – Note that the float valuesstrings involved here are better for that. It is not needed for communication – another streaming write can be donein fixed buffers, rather than dynamically allocated buffers like the String class uses.

Edit 1b: Streaming.h as-is doesn't build any strings, but just delivers the text of its <<-arguments character-by-character to a stream.

A StringStream class at github can build strings from stream inputs, so for example if sst is a StringStream object,

sst << "After " << millis() << " milliseconds, we're at " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

will build a string internal to sst, which can be read out character by character using StringStream's read() method, or if you modify the class, can be read out however you like.

In the larger scheme of things perhaps there's no need to create and save a formatted string like that. It is not useful for computing – the float values are better for that. It is not needed for communication – another streaming write can be done.

[Part of] Edit 1b: In the larger scheme of things perhaps there's no need to create and save a formatted string like that. It is not useful for computing – the float values are better for that. It is not needed for communication – another streaming write can be done.


Edit 2 [replaces most of 1b]: Streaming.h doesn't build any strings, but just delivers the text of its <<-arguments character-by-character to a stream. However, a PString class at arduiniana can build strings from stream inputs, as in the following example code.

#include <Streaming.h>
#include <PString.h>
char buffer[28];
PString pstr(buffer, sizeof(buffer));
void setup() {
 Serial.begin(115200);
 pstr.begin();  // Empty the buffer
}
void loop() {
 int first=50, second=100;
 pstr.begin(); // Empty the buffer
 pstr << 'X' << first << 'Y' << second << 'T';
  pstr += " hihi ";
 pstr << millis() << endl;
 Serial << pstr;
 delay(1000);
}

Here's what some of its output to Serial looks like:

X50Y100T hihi 263198
X50Y100T hihi 264199
X50Y100T hihi 265199
X50Y100T hihi 266201
X50Y100T hihi 267201
X50Y100T hihi 268201

What the sample of code does is first initialize a string buffer, then build part of a character string using streaming operator <<, then part of it using concatenation, then some more with <<, then print the string. Note that the strings involved here are in fixed buffers, rather than dynamically allocated buffers like the String class uses.

edit 1
Source Link

When formatting serial output is a concern, it's worthwhile to download and use the Streaming.h contributed library that adds some "syntactic sugar" to Arduino C. That is, at compile time it converts C++-like << Serial stream operators to Serial.print statements, without increasing code size. If you don't have Streaming.h installed, you can get Streaming5.zip from arduiniana.org.

At present, Streaming.h doesn't support setw() and setfill(), which in ordinary C++ set a field width and a fill character.

However, it does support four base-conversion specifiers, _HEX, _DEC, _OCT, and _BIN; a _FLOAT function with number of decimal places specified; and endl.

Thus, to print "Your coordinates are -23.123456, 135.123456" one would write:

Serial << "Your coordinates are " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

Note that unlike the String class, use of Streaming.h does not entail any dynamic memory management – everything it does happens at compile time – and it does not increase code size – it translates stream-style operations into ordinary .print() and .println() calls. It should result in somewhat smaller, faster, and more-robust code, by avoidance of String. Use of dynamic memory on small-memory embedded systems can be problematic, and this avoids those problems.


Edit 1a: As Chris Stratton points out, Arduino floats don't support 9-digit precision. I think Arduino floating-point arithmetic is intended to comply with IEEE 754 , which sets out standards to be used on modern computers that support floating-point arithmetic.

In the binary32 format used to implement IEEE 754 single-precision binary floating-point, significand precision is 24 bits. That equates to about 7.22 decimal digits (ie, 24*log10(2)), so for simple calculations, 7-digit accuracy is realistic, more than that is not.


Edit 1b: Streaming.h as-is doesn't build any strings, but just delivers the text of its <<-arguments character-by-character to a stream.

A StringStream class at github can build strings from stream inputs, so for example if sst is a StringStream object,

sst << "After " << millis() << " milliseconds, we're at " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

will build a string internal to sst, which can be read out character by character using StringStream's read() method, or if you modify the class, can be read out however you like.

In the larger scheme of things perhaps there's no need to create and save a formatted string like that. It is not useful for computing – the float values are better for that. It is not needed for communication – another streaming write can be done.

When formatting serial output is a concern, it's worthwhile to download and use the Streaming.h contributed library that adds some "syntactic sugar" to Arduino C. That is, at compile time it converts C++-like << Serial stream operators to Serial.print statements, without increasing code size. If you don't have Streaming.h installed, you can get Streaming5.zip from arduiniana.org.

At present, Streaming.h doesn't support setw() and setfill(), which in ordinary C++ set a field width and a fill character.

However, it does support four base-conversion specifiers, _HEX, _DEC, _OCT, and _BIN; a _FLOAT function with number of decimal places specified; and endl.

Thus, to print "Your coordinates are -23.123456, 135.123456" one would write:

Serial << "Your coordinates are " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

Note that unlike the String class, use of Streaming.h does not entail any dynamic memory management – everything it does happens at compile time – and it does not increase code size – it translates stream-style operations into ordinary .print() and .println() calls. It should result in somewhat smaller, faster, and more-robust code, by avoidance of String. Use of dynamic memory on small-memory embedded systems can be problematic, and this avoids those problems.

When formatting serial output is a concern, it's worthwhile to download and use the Streaming.h contributed library that adds some "syntactic sugar" to Arduino C. That is, at compile time it converts C++-like << Serial stream operators to Serial.print statements, without increasing code size. If you don't have Streaming.h installed, you can get Streaming5.zip from arduiniana.org.

At present, Streaming.h doesn't support setw() and setfill(), which in ordinary C++ set a field width and a fill character.

However, it does support four base-conversion specifiers, _HEX, _DEC, _OCT, and _BIN; a _FLOAT function with number of decimal places specified; and endl.

Thus, to print "Your coordinates are -23.123456, 135.123456" one would write:

Serial << "Your coordinates are " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

Note that unlike the String class, use of Streaming.h does not entail any dynamic memory management – everything it does happens at compile time – and it does not increase code size – it translates stream-style operations into ordinary .print() and .println() calls. It should result in somewhat smaller, faster, and more-robust code, by avoidance of String. Use of dynamic memory on small-memory embedded systems can be problematic, and this avoids those problems.


Edit 1a: As Chris Stratton points out, Arduino floats don't support 9-digit precision. I think Arduino floating-point arithmetic is intended to comply with IEEE 754 , which sets out standards to be used on modern computers that support floating-point arithmetic.

In the binary32 format used to implement IEEE 754 single-precision binary floating-point, significand precision is 24 bits. That equates to about 7.22 decimal digits (ie, 24*log10(2)), so for simple calculations, 7-digit accuracy is realistic, more than that is not.


Edit 1b: Streaming.h as-is doesn't build any strings, but just delivers the text of its <<-arguments character-by-character to a stream.

A StringStream class at github can build strings from stream inputs, so for example if sst is a StringStream object,

sst << "After " << millis() << " milliseconds, we're at " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

will build a string internal to sst, which can be read out character by character using StringStream's read() method, or if you modify the class, can be read out however you like.

In the larger scheme of things perhaps there's no need to create and save a formatted string like that. It is not useful for computing – the float values are better for that. It is not needed for communication – another streaming write can be done.

Source Link

When formatting serial output is a concern, it's worthwhile to download and use the Streaming.h contributed library that adds some "syntactic sugar" to Arduino C. That is, at compile time it converts C++-like << Serial stream operators to Serial.print statements, without increasing code size. If you don't have Streaming.h installed, you can get Streaming5.zip from arduiniana.org.

At present, Streaming.h doesn't support setw() and setfill(), which in ordinary C++ set a field width and a fill character.

However, it does support four base-conversion specifiers, _HEX, _DEC, _OCT, and _BIN; a _FLOAT function with number of decimal places specified; and endl.

Thus, to print "Your coordinates are -23.123456, 135.123456" one would write:

Serial << "Your coordinates are " << _FLOAT(latitude,6) << ", " << _FLOAT(longitude,6) << endl;

Note that unlike the String class, use of Streaming.h does not entail any dynamic memory management – everything it does happens at compile time – and it does not increase code size – it translates stream-style operations into ordinary .print() and .println() calls. It should result in somewhat smaller, faster, and more-robust code, by avoidance of String. Use of dynamic memory on small-memory embedded systems can be problematic, and this avoids those problems.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /