Velocity Reviews > floating point values

# floating point values

aegis
Guest
Posts: n/a

 12-14-2004
how can I take the fractional part of a floating point value
such that I can store that value into an integer type?

float foo = 6.180;

now take the fractional part
such that you can store 180 into an object
of type int or any other integer type.

I'm looking for way not involving the use of
float -> string -> parse string -> get 180 -> convert to int
but a process involving
float -> int

Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a

 12-14-2004
aegis <(E-Mail Removed)> wrote:
> how can I take the fractional part of a floating point value
> such that I can store that value into an integer type?

> float foo = 6.180;

> now take the fractional part
> such that you can store 180 into an object
> of type int or any other integer type.

> I'm looking for way not involving the use of
> float -> string -> parse string -> get 180 -> convert to int
> but a process involving
> float -> int

Use modf() to get the fraction, multiply by 1000 and round to the
nearest integer. Homework?
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de

Mike Wahler
Guest
Posts: n/a

 12-14-2004

"aegis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> how can I take the fractional part of a floating point value
> such that I can store that value into an integer type?

The fractional portion of a floating-point value will always
be less than 1, so when converted to integer, it will always
be zero.

>
> float foo = 6.180;
>
> now take the fractional part
> such that you can store 180 into an object
> of type int or any other integer type.

This requires *changing* this value (via multiplication)
from .180 to 180. IOW this does not involve a 'conversion'
but a modification.

>
> I'm looking for way not involving the use of
> float -> string -> parse string -> get 180 -> convert to int
> but a process involving
> float -> int

Then you'll be subject to the inherent inaccuracy of binary
floating point, and must allow for and correct for it.

#include <math.h>
#include <stdio.h>

int main()
{
float foo = 6.180f;
double ipart = 0;
double frac = modf(foo, &ipart);
int ifrac = 0;
char s[30] = {0};
float adj = 0.005f; /* for rounding */

printf("floating point value: %.3f\n"
"integer portion: %.3f\n"
"fractional portion: %.3f\n",
foo, ipart, frac);

ifrac = (int)(frac * 1000 + adj);

return 0;
}

Output:

floating point value: 6.18
integer portion: 6.000
fractional portion: 0.180

-Mike

Andrey Tarasevich
Guest
Posts: n/a

 12-14-2004
aegis wrote:
> how can I take the fractional part of a floating point value
> such that I can store that value into an integer type?
>
> float foo = 6.180;
>
> now take the fractional part
> such that you can store 180 into an object
> of type int or any other integer type.

Sorry, but that doesn't exactly make sense. As a number, '6.180' is not
different from '6.18', '6.18000', '6.1800' etc. You are saying that
fractional part of value '6.180' is '0.180', right? But why not '0.1800'
or '0.180000000'?

You need to explain more clearly what is it you are trying to do.

--
Best regards,
Andrey Tarasevich

Peter Nilsson
Guest
Posts: n/a

 12-14-2004
Mike Wahler wrote:
>
> #include <math.h>
> #include <stdio.h>
>
> int main()
> {
> float foo = 6.180f;
> double ipart = 0;
> double frac = modf(foo, &ipart);
> int ifrac = 0;
> char s[30] = {0};

round.c:10: warning: unused variable `s'

> float adj = 0.005f; /* for rounding */

ITYM 0.5f

>
> printf("floating point value: %.3f\n"
> "integer portion: %.3f\n"
> "fractional portion: %.3f\n",
> foo, ipart, frac);
>
> ifrac = (int)(frac * 1000 + adj);

This is somewhat simplistic, i.e. it may not round as expected if foo
(and thus possibly frac) is negative.

>
> return 0;
> }

--
Peter

Mike Wahler
Guest
Posts: n/a

 12-14-2004

"Peter Nilsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Mike Wahler wrote:
> >
> > #include <math.h>
> > #include <stdio.h>
> >
> > int main()
> > {
> > float foo = 6.180f;
> > double ipart = 0;
> > double frac = modf(foo, &ipart);
> > int ifrac = 0;
> > char s[30] = {0};

>
> round.c:10: warning: unused variable `s'

I meant to take that out.

>
> > float adj = 0.005f; /* for rounding */

>
> ITYM 0.5f

No. We're rounding to nearest thousand, not one.
OF course this has the 'fragility' that it doesn't
handle other numbers of wanted digits after the
decimal point. But that's OP's problem.

>
> >
> > printf("floating point value: %.3f\n"
> > "integer portion: %.3f\n"
> > "fractional portion: %.3f\n",
> > foo, ipart, frac);
> >
> > ifrac = (int)(frac * 1000 + adj);

>
> This is somewhat simplistic, i.e. it may not round as expected if foo
> (and thus possibly frac) is negative.

Yes, it is simplistic. Had not the OP specifically disallowed it,
I would have advised creating a formatted string, parsing that
for the '.', and converting the desired subsequent characters
to type 'int'.

>
> > printf("adjusted result: %d\n", ifrac);
> >
> > return 0;
> > }

-Mike

Lawrence Kirby
Guest
Posts: n/a

 12-15-2004
On Tue, 14 Dec 2004 20:35:23 +0000, Mike Wahler wrote:

....

>> > float adj = 0.005f; /* for rounding */

>>
>> ITYM 0.5f

>
> No. We're rounding to nearest thousand, not one.

You round after scaling so you would at that point be trying to round
to the nearest integer. It is also handy that 0.5 is exactly representable
in binary floating point.

Lawrence