Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > truncating a floating type variable

Reply
Thread Tools

truncating a floating type variable

 
 
VISHNU VARDHAN REDDY UNDYALA
Guest
Posts: n/a
 
      10-23-2005
Hello,
Can someone over here help me in truncating a float variable.
I mean if PI=3.14159 ...How can I get to read the first two or first
three decimal values with out rounding them. Any suggestions are
appreciated. I am BEGINNER TO C PROGRAAMING. Thanks

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      10-23-2005
On 23 Oct 2005 07:59:37 -0700, "VISHNU VARDHAN REDDY UNDYALA"
<> wrote:

>Hello,
>Can someone over here help me in truncating a float variable.
>I mean if PI=3.14159 ...How can I get to read the first two or first
>three decimal values with out rounding them. Any suggestions are
>appreciated. I am BEGINNER TO C PROGRAAMING. Thanks


Your question is very ambiguous. First you talk about truncating a
floating point variable. Then you talk about reading data. Describe
what you really want to do.

When composing your question, consider the following:

There are many systems for representing floating point variables.
I do not know of any that use decimal. In all of the binary systems I
am familiar with, most decimal values cannot be represented exactly.
Thus, the value 3.14 will be stored in a float (or double) as some
value that is close to 3.14 but that has an "error" in the outer
decimal positions, something like 3.14000004 or 3.13999998.

What is your starting point? Is the value of pi already in a
float or double? Is it in a file to be read? If so, it what format
(character, binary, etc)?

What do you want the result to be? Another floating point value?
How many significant digits? A character string? How long?


<<Remove the del for email>>
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      10-23-2005
Barry Schwarz wrote:
> On 23 Oct 2005 07:59:37 -0700, "VISHNU VARDHAN REDDY UNDYALA"
> <> wrote:
>
>
>>Hello,
>>Can someone over here help me in truncating a float variable.
>>I mean if PI=3.14159 ...How can I get to read the first two or first
>>three decimal values with out rounding them. Any suggestions are
>>appreciated. I am BEGINNER TO C PROGRAAMING. Thanks

>
>
> Your question is very ambiguous. First you talk about truncating a
> floating point variable. Then you talk about reading data. Describe
> what you really want to do.
>
> When composing your question, consider the following:
>
> There are many systems for representing floating point variables.
> I do not know of any that use decimal.


There are, for the finance sector; see for example
http://www2.hursley.ibm.com/decimal/

> In all of the binary systems I
> am familiar with, most decimal values cannot be represented exactly.
> Thus, the value 3.14 will be stored in a float (or double) as some
> value that is close to 3.14 but that has an "error" in the outer
> decimal positions, something like 3.14000004 or 3.13999998.
>
> What is your starting point? Is the value of pi already in a
> float or double? Is it in a file to be read? If so, it what format
> (character, binary, etc)?
>
> What do you want the result to be? Another floating point value?
> How many significant digits? A character string? How long?


I am not sure but I think the Op either wants
targetnumber = floor(PI*100)/100;
or
snprintf(targetstring, 4, "%g", PI);
where the 4 stems from '3', '.', and two additional digits.

This assumes PI >= 0.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
VISHNU VARDHAN REDDY UNDYALA
Guest
Posts: n/a
 
      10-23-2005
Hi Barry,
The value of pi is already in a float ... and the output comes till
like 6-7 decimal points. I want the value of pi till 2 decimal points
with out getting round off ... and which could be assigned to another
variable (any). Thanks
vishnu

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-24-2005
On 23 Oct 2005 15:16:36 -0700, "VISHNU VARDHAN REDDY UNDYALA"
<> wrote:

>Hi Barry,
>The value of pi is already in a float ... and the output comes till
>like 6-7 decimal points. I want the value of pi till 2 decimal points
>with out getting round off ... and which could be assigned to another
>variable (any). Thanks
>vishnu


You need to quote some context. Not everyone will remember what you
talked about in a previous message.

Use sprintf to convert the value to a string. Truncate the string
after the fourth character by storing a '\0' in the fifth position.
Use strtod or sscanf to evaluate the string and place the value in the
numeric variable of your choice. Be aware that the value will not be
exact for the reasons I discussed in my first message.


<<Remove the del for email>>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-24-2005
"VISHNU VARDHAN REDDY UNDYALA" <> writes:
> The value of pi is already in a float ... and the output comes till
> like 6-7 decimal points. I want the value of pi till 2 decimal points
> with out getting round off ... and which could be assigned to another
> variable (any). Thanks


Don't assume that your readers are able to see the article to which
you're replying.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Please complain to Google about their broken interface.

What exactly do you mean by "without getting round off"? Do you mean
that you want the result truncated rather than rounded, so for 4
decimal placed you get 3.1415 rather than 3.1416?

As Barry Schwarz already told you, what you're asking for is basically
not possible. Given a floating-point value approximating pi, you can
obtain a value that *approximates* the value of pi truncated to 2
decimal places (3.14), but the exact value 3.14 cannot be represented
in binary floating-point.

Here's a program that demonstrates the problem. (Note that I used
double rather than float.)

#include <math.h>
#include <stdio.h>
int main(void)
{
double pi = 4.0 * atan(1.0);
double pi2 = floor(pi * 100.0) / 100.0;
double pi4 = floor(pi * 10000.0) / 10000.0;
printf("pi = %f, really %.20f\n", pi, pi);
printf("pi2 = %f, really %.20f\n", pi2, pi2);
printf("pi4 = %f, really %.20f\n", pi4, pi4);
return 0;
}

And here's the output I got (it may be slightly different on your
system):

pi = 3.141593, really 3.14159265358979311600
pi2 = 3.140000, really 3.14000000000000012434
pi4 = 3.141500, really 3.14150000000000018119

Presumably truncating the value like this is a means to an end rather
than an end in itself. What exactly are you trying to accomplish?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
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
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
Truncating Variables Jason Williard ASP .Net 11 10-18-2004 01:27 PM
DVD Verdict reviews: A STORY OF FLOATING WEEDS / FLOATING WEEDS: CRITERION COLLECTION and more! DVD Verdict DVD Video 0 04-20-2004 09:04 AM
System.web.mail truncating message body - strange behaviour ?? Jitesh Sinha ASP .Net 1 12-05-2003 03:04 PM
JavaService append mode instead of truncating log file uy_do Java 1 12-04-2003 02:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57