473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

truncating a floating type variable

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

Nov 15 '05 #1
5 5405
On 23 Oct 2005 07:59:37 -0700, "VISHNU VARDHAN REDDY UNDYALA"
<uv*****@gmail.com> 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>>
Nov 15 '05 #2
Barry Schwarz wrote:
On 23 Oct 2005 07:59:37 -0700, "VISHNU VARDHAN REDDY UNDYALA"
<uv*****@gmail.com> 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.
Nov 15 '05 #3
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

Nov 15 '05 #4
On 23 Oct 2005 15:16:36 -0700, "VISHNU VARDHAN REDDY UNDYALA"
<uv*****@gmail.com> 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>>
Nov 15 '05 #5
"VISHNU VARDHAN REDDY UNDYALA" <uv*****@gmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Dylan Nicholson | last post by:
I just posted regarding a possible floating point error (not sure where), and have since discovered that: float f = 5.15002; double d = 5.15002; if (float(d) < f) puts("huh 1?"); float f2 =...
21
by: Allin Cottrell | last post by:
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A...
11
by: Jason Williard | last post by:
Is there a built-in function that I can use to truncate variables longer than 30 characters? Even better, is there a function that truncates and then appends "(...)" to the end? -- Thank...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
1
by: mmt1 | last post by:
Hi. I have a multiplier that multiplies 2 floating point numbers with 7 bits exponent and 10 bits mantissa.so its output has 7 bits exponent and 20 bits mantissa. now its output must return to...
70
by: Robert Gamble | last post by:
9899:1999 5.1.2.3 Example 4 reads: "EXAMPLE 4 Implementations employing wide registers have to take care to honor appropriate semantics. Values are independent of whether they are represented in a...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
4
by: Todd | last post by:
I have a function that takes a double and truncates it to a specified number of decimal places. I've found that I have to add a small number to the input value otherwise I get errors due to the...
15
geolemon
by: geolemon | last post by:
I'm having a seriously Twilight Zone moment: In the code below, I'm building a SQL command into a string variable, declared at the top of my code. Then, I connect to the database and try to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.