LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

truncate a float number

 I would like to truncate a float number 1234. 056789 back to float number but with a desired fraction as the following:
 
1.  numb = truncate(1234. 056789, 3)  ---> numb = 1234.056
2.  numb = truncate(1234. 056789, 5)  ---> numb = 1234.05678
 
I did try:
 
float math_truncate(float numb, int n_trunc)
{
      int   i, n = pow(10,n_trunc);
      double num;
 
      i = (int)(numb * n); 
  
      num = i/(n * 1.0); 
 
     return (float)num;
}
 
Before return I have:
 
Case #1: num = 1234.056000000 ... after return, I have 1234.056107
Case #2: num = 1234.056780000 ... after return, I have 1234.05678107
 
Note1: I did try other methods, but the results are not as #1 & #2 ... always get some after the desired decimal point
Note2: The TruncateRealNumber (number) only truncate all fraction part ... num = 1234.00000)
 
*) Anyone can help?
0 Kudos
Message 1 of 8
(5,498 Views)

Your truncate function will not work for all numbers based on the way real numbers are stored in a computer.

Why do you want to truncate the number?  To compare values?  Click <a href="http://forums.ni.com/ni/board/message?board.id=180&message.id=26703#M26703">here</a> to see  a discussion on comparing float values.

Click <a href="http://forums.ni.com/ni/board/message?board.id=180&message.id=591&query.id=39069#M591">here</a>  for some additional discussion on the precision of floating point numbers.

If you want to truncate the number for display purposes, you can do something like the following in ANSI C.

double myDouble;
char myString[256];
myDouble = 3.1415926535;
printf("%f truncated to 2 decimal places = %.2f\n", myDouble, myDouble);
sprintf(myString, "%.2f", myDouble);
printf("%f truncated to 2 decimal places and written to a string = %s\n", myDouble, myString);

0 Kudos
Message 2 of 8
(5,492 Views)

It is the assigned task I have to do, I can not use printf (or others to display) ... but the task must be done that way!

If it so difficult, then if I have an integer number 12345 can I convert to an float as 123.45, to 12.345 ... if we can do these converts then that is the same solution! As you see I did try to divide it to a proper number (100 or 1000), but it failed!Smiley Sad

0 Kudos
Message 3 of 8
(5,486 Views)

The closest representation you will be able to get with a "double" is to multiply the value by 10^n where n is your required number of digits after the decimal point.  Then use RoundRealToNearestInteger() to convert the result to a integer value, then assign the integer result back to a double and divide by 10^n.  This can however still not round exactly to the value you want because of the method in which a value is stored as a double as mentioned before.

If you do have a requirement to maintain the values independent of the error induced by using a double, you will need to convert your values to integers (longs probably) and maintain the location of the decimal point separately.   Sounds like a great CS class programming problem.

Message Edited by mvr on 01-05-2007 02:49 PM

0 Kudos
Message 4 of 8
(5,483 Views)

All numbers in a computer are represented in binary.  Integers can be represented exactly in binary.  Floating point numbers can have a small error when they are represented in binary.

Don't think that the error in floating point precision can be solved by a few lines of code.  PhD dissertations have been written on this.  The IEEE spent years defining the standard for floating point representation.

As mvr says, you can convert everything to integers and then truncate, but you'll still have the same problem going back to floating point.

What are your real requirements?

0 Kudos
Message 5 of 8
(5,476 Views)

Sorry about I did not clarify that the number to truncate is a float (not a double), in my codes I had to use as double for resulting all 0's after dividing with a number (ie: 1234.56000, if I use as a float it would be 1234.56107 ...) ---> result must be a float

Note: We should have a way to do it (but I have  not known), because someone did do it ... but they do not use ANSI_C or CVI!

0 Kudos
Message 6 of 8
(5,476 Views)

The bottom line is that floats do not have enough "precision" to avoid some pretty significant error under some conditions.  This is not really a limitation of C it is a standard programming problem.   BCD math is one way this is handled in systems that cannot tolerate the error induced by binary floating point representations.

http://www2.hursley.ibm.com/decimal/

Message Edited by mvr on 01-05-2007 03:17 PM

Message 7 of 8
(5,473 Views)
Thanks for the explaination & the link, I will study it! Smiley Surprised
0 Kudos
Message 8 of 8
(5,462 Views)