Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > floating point values

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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);
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



 
Reply With Quote
 
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
 
Reply With Quote
 
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.

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


--
Peter

 
Reply With Quote
 
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


 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
floating point to fixed point conversion riya1012@gmail.com C Programming 4 02-22-2006 05:56 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments