Pages: [1]   Go Down
Author Topic: Issue with Float/Double in Arduino  (Read 1228 times)
0 Members and 1 Guest are viewing this topic.
bangalore
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey folks,

i was not able to come out of the shock that in Arduino float/double supports only two digits after decimal.! even though it was mentioned 6-7digits of precision including the digits before and after the decimal. can someone help me to overcome this problem.

   i wanted to read a value if 0.009, 0.000008 etc...such kindof  values...

any help is appriciated..!

thnq,
chiru
Logged

chiru

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While I haven't found anything for this, using some simple notation would be easy. Ex: 9/100 or someVar/100
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 198
Posts: 12753
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i was not able to come out of the shock that in Arduino float/double supports only two digits after decimal.!

Prove it.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey folks,

i was not able to come out of the shock that in Arduino float/double supports only two digits after decimal.! even though it was mentioned 6-7digits of precision including the digits before and after the decimal. can someone help me to overcome this problem.

   i wanted to read a value if 0.009, 0.000008 etc...such kindof  values...

any help is appriciated..!

thnq,
chiru

I think you are confusing the serial print options for floating numbers Vs the underlining percision of the arduino float variables, those are two very different things.

From the serial reference:

Quote
Syntax
Serial.print(val)
Serial.print(val, format)


Parameters
val: the value to print - any data type


format: specifies the number base (for integral data types) or number of decimal places (for floating point types)


So you can see you must supply the format parameter if you want to print for other then the default value of two digits past the decimal point.

As far as the percision of the float type used in arduino, also from the reference section:

Quote
Floats have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point. Unlike other platforms, where you can get more precision by using a double (e.g. up to 15 digits), on the Arduino, double is the same size as float.


You should be able to work out the range and percision of floats you are working with in your sketch with those two key references. Printing for example .00067 should not be a problem, as long as you format your print statement correctly.

« Last Edit: February 01, 2012, 01:16:23 am by retrolefty » Logged

bangalore
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thnq folks....i could overcome this problem now.

thnq all
Logged

chiru

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd describe the issue as a bug in the Serial print function myself - default options should not be hiding your values from you.
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'd describe the issue as a bug in the Serial print function myself - default options should not be hiding your values from you.
I disagree; the "bug" is documented on the reference page.
http://arduino.cc/en/Serial/Print
"For floating point numbers, this parameter specifies the number of decimal places to use."
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@MarkT, what should be the default number of decimals for float in your opinion? (just curious)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

bangalore
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 the default number after decimal for float/double in Arduino is 2
Logged

chiru

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@MarkT, what should be the default number of decimals for float in your opinion? (just curious)

All of them, of course.  Every other programming language I can think of does this.
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd describe the issue as a bug in the Serial print function myself - default options should not be hiding your values from you.

Well, that's what defaults do, right?

From the documentation:

Quote
Floats are similarly printed as ASCII digits, defaulting to two decimal places.

Although I like this bit:

Quote
Programming Tips
As of version 1.0, serial transmission is asynchronous; Serial.print() will return before any characters are transmitted.

They forgot to mention: "until the buffer fills up".
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
All of them, of course.  Every other programming language I can think of does this.
Has anyone tried this with a bare "%f" in C ?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <stdio.h>

int main ()
{
  float f = 12.3456789;

  printf ("f is %f\n", f);
  return 0;

}

Output:

Code:
f is 12.345679
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Microsoft's 'C' runtime library is specified to print six decimal places by default. (Actually, they say it produces six "digits of precision" but I think that's a typo because the accompanying examples show six decimal places.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: