floating point values

A

aegis

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
 
J

Jens.Toerring

aegis said:
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
 
M

Mike Wahler

aegis said:
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);
printf("adjusted result: %d\n", ifrac);

return 0;
}

Output:

floating point value: 6.18
integer portion: 6.000
fractional portion: 0.180
adjusted result: 180

-Mike
 
A

Andrey Tarasevich

aegis said:
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.
 
P

Peter Nilsson

Mike said:
#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.
 
M

Mike Wahler

Peter Nilsson said:
Mike said:
#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. :)
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. :)
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'.

-Mike
 
L

Lawrence Kirby

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

....
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top