Go Down

Topic: String constructor doesn't convert float type correctly (Read 308 times) previous topic - next topic

ArtSmart

If you specify more than 7 decimal places for string constructor it doesn't convert it properly.

Code: [Select]
  String fltstring;
  float test = 0.000123456789;
  fltstring = String(test,7);
  Serial.print("Test String: ");
  Serial.println(fltstring)
;

The code above produces expected output:

Test String: 0.0001234


Now if we change the number of decimal places to 8:

Code: [Select]
  String fltstring;
  float test = 0.000123456789;
  fltstring = String(test,8);
  Serial.print("Test String: ");
  Serial.println(fltstring)
;

We get:

Test String: 0.00012346


For 9 decimal places we get output:

Test String: 0.000123457
 

I can understand if the constructor limits conversion to 7 decimal places (although it is not mentioned anywhere).  However it still should handle correctly float type if more decimal places are specified.


Delta_G

Floats aren't accurate past 6 or 7 places. The String constructor is accurately handling the float variable, but the float variable isn't accurately holding the number you're trying to put in.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

ArtSmart

Floats aren't accurate past 6 or 7 places. The String constructor is accurately handling the float variable, but the float variable isn't accurately holding the number you're trying to put in.
Incorrect.  Floats are accurate up to ~7 non-zero places.  All leading zeros will be dropped after exponential scaling.  In other words you can have as many leading zeros before first non-zero digit and still have ~7 digits after them.  I intentionally used four leading zeros in my example.  The number 1234567e-10 should be correctly represented by a float variable and convert to 0.000123456, which, as you can see in the example above, is not the case.  It seems that the conversion that's taking place in the the constructor incorrectly takes first six decimal places even if they are zeros and then does something funky with the last digit.


PaulS

Quote
It seems that the conversion that's taking place in the the constructor incorrectly takes first six decimal places even if they are zeros and then does something funky with the last digit.
You do have the source code, so there is no reason to guess what the String class is doing.

Delta_G

Incorrect.  Floats are accurate up to ~7 non-zero places.  All leading zeros will be dropped after exponential scaling.  In other words you can have as many leading zeros before first non-zero digit and still have ~7 digits after them.  I intentionally used four leading zeros in my example.  The number 1234567e-10 should be correctly represented by a float variable and convert to 0.000123456, which, as you can see in the example above, is not the case.  It seems that the conversion that's taking place in the the constructor incorrectly takes first six decimal places even if they are zeros and then does something funky with the last digit.


To me it looks like it rounded off in the last place.

What were you expecting the output to be for 8 or 9 places?  Try with test = 0.000987654321 and see why it's different. Try printing that to only 4 or 5 places.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

Delta_G

Incorrect.  Floats are accurate up to ~7 non-zero places.  All leading zeros will be dropped after exponential scaling.  In other words you can have as many leading zeros before first non-zero digit and still have ~7 digits after them.  I intentionally used four leading zeros in my example.  The number 1234567e-10 should be correctly represented by a float variable and convert to 0.000123456, which, as you can see in the example above, is not the case.  It seems that the conversion that's taking place in the the constructor incorrectly takes first six decimal places even if they are zeros and then does something funky with the last digit.


Your value for test has 9 significant figures.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

liudr

Like mentioned before, read the source code. My guess is the digit specified get used to generate digits after decimal.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy