Pages: [1]   Go Down
 Author Topic: How to truncate a float value  (Read 8204 times) 0 Members and 1 Guest are viewing this topic.
0
Offline
Newbie
Karma: 0
Posts: 49
Arduino rocks
 « on: February 12, 2008, 07:22:15 pm » Bigger Smaller Reset

What is the correct syntax to truncate a float value?  For instance, if I have a float value of 3.1456987456321156 and I want it to actually be 3.14 -- what is the syntax in Arduino for that?

 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #1 on: February 13, 2008, 08:32:37 am » Bigger Smaller Reset

If you want to format your number as output, sprintf is the usual thing to use. Have a google on setting width and precision in printf or sprintf.

I can't imagine any reason to truncate a floating point number in a calculation, but if you want to do it, something like this for a floating point  value f should get you close

((int)(f * 100 ))/ 100.0
 Logged

0
Offline
Newbie
Karma: 0
Posts: 49
Arduino rocks
 « Reply #2 on: February 13, 2008, 10:03:38 am » Bigger Smaller Reset

Well, I know that floating points are difficult to use because they carry out to so many decimal points that comparing two numbers for similarity will almost certainly result in the numbers not being the same.  If I can truncate it down to only two decimal places, that will be enough to determine if two results are the same or not.  After three or four decimal places, my numbers will start to change, but I dont care about that much detail for my project.

I am converting analog readings (0-1052)  to velocity readings which will only change slightly and result in fractions or rather decimals.  So, I want to use float, but only carry to two decimal places.  I'm just not familiar with C/C++ and it's syntax.
 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #3 on: February 13, 2008, 10:39:03 am » Bigger Smaller Reset

if you want to see if two floating point number are the same up to the first two decimal places you could call a function like this:

Code:
boolean isEqual(float f1, float f2){
return ( (int)(f1 *100)) == ((int)(f2 * 100) );
}

this should return true if the two floats passed are the same up the first two decimal places. If your floating point numbers will be bigger than the largest signed integer divided by 100  (i.e. 327 ) then change the cast of (int) to (long) in both places in the function

There may actually be a C library function to do this but I avoid using floats if I can, so I have not needed to look for one
 « Last Edit: February 13, 2008, 10:43:15 am by mem » Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #4 on: February 13, 2008, 11:01:03 am » Bigger Smaller Reset

BTW, You can achieve the same thing more efficiently by doing your entire calculation using integers (or longs) and multiplying the numerator by 100 before you do any division.

i.e. to calculate     velocity =  distance  / time

where distance =  51
and       time =  31

the floating point result is 1.64516

but if you use just integers (or longs) and multiply the numerator by 100 you get:

5100 / 31 =  164

Which is the velocity rounded off to two decimal places times 100.  This is much more efficiently calculated by the arduino hardware, uses less memory and is easier to compare than the floating point  version.

Your logic just needs to take into account that your results are not in (for example) meters per second but in centimetres per second.
 « Last Edit: February 13, 2008, 11:07:13 am by mem » Logged

Copenhagen / Denmark
Offline
Edison Member
Karma: 6
Posts: 2360
Do it !
 « Reply #5 on: February 14, 2008, 07:41:33 am » Bigger Smaller Reset

There's a little confusion here:

Truncating usually means removing EVERYTHING after the decimal point.

What you want (as far as i can see) is to round your float to 2 decimals.

 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #6 on: February 14, 2008, 08:12:55 am » Bigger Smaller Reset

because the ADC has only 10 bits of precision it probably doesn't make any difference if the comparison is done on a number rounded before truncating or not. But if it does then .005 could be added to the floats before truncating.
 « Last Edit: February 14, 2008, 08:21:19 am by mem » Logged

0
Offline
Newbie
Karma: 0
Posts: 49
Arduino rocks
 « Reply #7 on: February 14, 2008, 01:59:33 pm » Bigger Smaller Reset

MikMo -- you're right.....I did mean to round off.
Mem -- thanks for the insight.  I think I can make your example work.  At least it makes sense in my head.  I'll have to see if i can put it on paper now.

Thanks again.
 Logged

 Pages: [1]   Go Up