linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Remove x number of decimals from a float value
@ 2006-07-11 18:10 Fabio Miranda Hamburger
  2006-07-11 19:08 ` Per Jessen
  2006-07-11 20:58 ` Glynn Clements
  0 siblings, 2 replies; 3+ messages in thread
From: Fabio Miranda Hamburger @ 2006-07-11 18:10 UTC (permalink / raw)
  To: linux-c-programming

Hello,

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.

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

I was trying to implement this function using datatype casting, abs() and 
other functions from math.h library.

Thanks for any advice,


---
Fabio Andres Miranda
Ingenieria de sistemas informaticos
Universidad Latina - Costa Rica


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Remove x number of decimals from a float value
  2006-07-11 18:10 Remove x number of decimals from a float value Fabio Miranda Hamburger
@ 2006-07-11 19:08 ` Per Jessen
  2006-07-11 20:58 ` Glynn Clements
  1 sibling, 0 replies; 3+ messages in thread
From: Per Jessen @ 2006-07-11 19:08 UTC (permalink / raw)
  To: linux-c-programming

Fabio Miranda Hamburger wrote:
> Hello,
> 
> 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.
> 
> 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
> 
> I was trying to implement this function using datatype casting, abs() 
> and other functions from math.h library.

How about :


double f( double x, int n )
{
         return ( n>0 ? f( x*10,n-1 )/10 : floor(x) );
}

Oops, 'n' is number of decimals you want to keep, not throw away.


/Per Jessen, Zurich

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Remove x number of decimals from a float value
  2006-07-11 18:10 Remove x number of decimals from a float value Fabio Miranda Hamburger
  2006-07-11 19:08 ` Per Jessen
@ 2006-07-11 20:58 ` Glynn Clements
  1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2006-07-11 20:58 UTC (permalink / raw)
  To: Fabio Miranda Hamburger; +Cc: linux-c-programming


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@gclements.plus.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-07-11 20:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-07-11 18:10 Remove x number of decimals from a float value Fabio Miranda Hamburger
2006-07-11 19:08 ` Per Jessen
2006-07-11 20:58 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).