Re: Remove x number of decimals from a float value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Fabio Miranda Hamburger wrote:

> I am trying to implement a 100% math function ( avoid handle the float as 
> a char array), that remove x number of decimals from the right to the 
> float point.

That is a meaningless specification.

> Example:
> Given x= 12345.6789 I call the function with (x,2) and the returned number 
> is: x= 12345.67, for (x,4) returned value is x=12345

So presumably if x = 12345.67890000, f(x,2) would be 12345.678900 and
f(x,4) would be 12345.6789.

But all of those numbers are the same.

Also, bear in mind that you cannot exactly represent 0.1 (or, more
generally, 10^(-n) for any n >= 1) in floating-point.

E.g. storing 12345.6789 as a single-precision value will result in:

	12345.67871093750000000000000000000000000000000000000000...

while a double-precision value will give:

	12345.67890000000079453457146883010864257812500000000000...

Per's suggestion of *keeping* a given number of decimal places is
feasible.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux