473,405 Members | 2,187 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,405 software developers and data experts.

Rounding Float in C and Remove those Zeros

Hi,

If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?

I then have to save the number into a variable.

The method appears below:
**************************************
printf("Round % .f \n", 10.0000000);


and the result will appear on the screen as "10"

is there any method to "save" the rounded value as X?

Thanks,

May 12 '06 #1
13 15506
ke********@yahoo.com.hk wrote:
Hi,
Hi!
If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?
Er, yes... maybe. But you don't say what `X` is: an `int`, a `double`?
What exactly do you want to achieve?
I then have to save the number into a variable.
What variable?
The method appears below:
**************************************
printf("Round % .f \n", 10.0000000);

This is not "saving a number into a variable".
and the result will appear on the screen as "10"
Yes (not necessarily on the "screen", though).
is there any method to "save" the rounded value as X?


Yes, but you'll have to be more specific about what exactly you want to
do.

May 12 '06 #2
Vladimir Oka opined:
ke********@yahoo.com.hk wrote:
Hi,


Hi!
If in C a variable appears like X=10.000000, I can round it to zero
decimal places. ie X=10?


Er, yes... maybe. But you don't say what `X` is: an `int`, a
`double`? What exactly do you want to achieve?
I then have to save the number into a variable.


What variable?
The method appears below:
**************************************
>printf("Round % .f \n", 10.0000000);
This is not "saving a number into a variable".
and the result will appear on the screen as "10"


Yes (not necessarily on the "screen", though).
is there any method to "save" the rounded value as X?


Yes, but you'll have to be more specific about what exactly you want
to do.


I have also received this reply from Ken:
Hi,

Thanks for your reply to my post in Google Groups.

So I have a prrogram as follows:

*************************
Float X;(which X is a float)
int Y;(which Y is an integer).

X = 10.0000;

what can I do so that I can transform 10.0000 into 10
and assign Y = 10?


Firstly, it is considered polite to keep all topical discussions
public. Many people don't even publish their (correct) e-mail
addresses.

In response to your question, to assign 10 to `Y`, you can do either
of:

Y = 10; /* facetious */

or

X = 10.0000;
Y = X;

In the latter, `Y` will indeed be 10 only if 10 is exactly
representable as a floating point number on your implementation. I
think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.

--
Persistence in one opinion has never been considered
a merit in political leaders.
-- Marcus Tullius Cicero, "Ad familiares", 1st century BC

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 12 '06 #3
"Vladimir Oka" writes:
I > think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.


No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O libraries
are aware of this and create the *illusion* that you can.
May 12 '06 #4
osmium opined:
"Vladimir Oka" writes:
I > think (before looking into the Standard) that all small integers
are guaranteed to be. Beware numbers like 1000000000000042.0000,
though.


No. For example, the number 1 can not be represented exactly. Select
as many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O
libraries are aware of this and create the *illusion* that you can.


Hmmm. A quick look into the Standard, and some searching through past
c.l.c posts seem to justify my statement that certain range of
integers is indeed exactly representable in floating point types.

Look at the <float.h>. It defines FLT_DIG, DBL_DIG and LDBL_DIG as
the number of decimal digits of precision in a `float`, `double` and
`long double`, respectively. Integers with less than this number of
digits should be exactly representable in the appropriate floating
point types.

I can't say much (anything?) about how this is exactly achieved,
though.

--
Too much of anything, even love, isn't necessarily a good thing.
-- Kirk, "The Trouble with Tribbles", stardate 4525.6

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 12 '06 #5
osmium wrote:
"Vladimir Oka" writes:
I think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.
No. For example, the number 1 can not be represented exactly.


You've happened to pick a number that's exactly representable in all
floating-point systems: 1.0 = 1 * b^0 for any valid base b.
Select as many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O libraries
are aware of this and create the *illusion* that you can.

This illusion is called rounding, and it's not really I/O specific. It
depends on the number of digits you ask for, and are able to get.

Had you said 0.1, you might have had a point, as this number is not exactly
representable in binary floating point. On the other hand, implementations
need not use base 2; 0.1 *is* exactly representable in base 10.

You may wish to verify that "all small integers" are indeed exactly
representable in every floating-point system. All integers from -(b^p)
through b^p, with b the base and p the precision, are exactly representable.
For b = 2 and p = 24 (an IEEE 754 single-precision float), all integers
from -16777216 through 16777216 are exactly representable. For double
precision, all integers from -(2^53) through 2^53 are exactly representable.

At this point I'm obliged to refer to the classic: "What Every Computer
Scientist Should Know About Floating-Point Arithmetic", available at, for
example,
http://www.physics.ohio-state.edu/~d...point_math.pdf

S.
May 12 '06 #6
In article <4c*************@individual.net> "osmium" <r1********@comcast.net> writes:
No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex


You clearly have no idea how floating point works.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
May 12 '06 #7
"Vladimir Oka" <no****@btopenworld.com> wrote

Look at the <float.h>. It defines FLT_DIG, DBL_DIG and LDBL_DIG as
the number of decimal digits of precision in a `float`, `double` and
`long double`, respectively. Integers with less than this number of
digits should be exactly representable in the appropriate floating
point types.

I can't say much (anything?) about how this is exactly achieved,
though.

Imagine that you are allowed to represent floating point digits in ASCII
using only six digits.

So a half is no problem. That's "0.5". A small whole number is also no
problem, that's "100" and so forth.
A third is a bit of a snag. We can write "0.3333" which is good enough for
most purposes, but not exact.
Also if we want very large numbers, we run out of digits. However we can go
to scientific notation. So 1234567890 would be "1.23e9". We've lost the low
digits, but we're still accurate to 3 significant figures.

Computer floating point works in a very similar way, except that the digits
are binary rather than decimal, and there isn't a literal point character -
the point is implicit in the scientific style of notation.
--
Website: www.personal.leeds.ac.uk/~bgy1mm

May 13 '06 #8
osmium wrote:
"Vladimir Oka" writes:
I > think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.


No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O libraries
are aware of this and create the *illusion* that you can.

You misunderstand the floating point system. In IEEE 754 integers up to
the width of the mantissa are represented exactly. On my system thats
2**24 for float and 2**53 for double.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 15 '06 #9
"Joe Wright" writes:
osmium wrote:

No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O
libraries are aware of this and create the *illusion* that you can.

You misunderstand the floating point system. In IEEE 754 integers up to
the width of the mantissa are represented exactly. On my system thats
2**24 for float and 2**53 for double.


You're right. I was referring to the floating point representation I
learned in school. Which was not THE floating point system, it was only A
floating point system. Since seeing some of the earlier posts I have looked
into IEEE 754 and it *is* quite different - and an improvement. Bystanders
should be aware, however, that there is no assurance that C uses IEEE 754,
so coders still have to follow the old rules if they want to permit cross
platform use. That is, don't use == if you want portability. The
representation I was speaking about is still permissible.
May 15 '06 #10
"osmium" <r1********@comcast.net> writes:
"Joe Wright" writes:
osmium wrote:
No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O
libraries are aware of this and create the *illusion* that you can.

You misunderstand the floating point system. In IEEE 754 integers up to
the width of the mantissa are represented exactly. On my system thats
2**24 for float and 2**53 for double.


You're right. I was referring to the floating point representation I
learned in school. Which was not THE floating point system, it was only A
floating point system. Since seeing some of the earlier posts I have looked
into IEEE 754 and it *is* quite different - and an improvement. Bystanders
should be aware, however, that there is no assurance that C uses IEEE 754,
so coders still have to follow the old rules if they want to permit cross
platform use. That is, don't use == if you want portability. The
representation I was speaking about is still permissible.


And how does this system you learned in school represent 2.0?

--
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.
May 15 '06 #11
osmium wrote:
"Joe Wright" writes:
osmium wrote:

No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex

No way are you going to get a 1 out of that. People who write I/O
libraries are aware of this and create the *illusion* that you can.

You misunderstand the floating point system. In IEEE 754 integers up to
the width of the mantissa are represented exactly. On my system thats
2**24 for float and 2**53 for double.


You're right. I was referring to the floating point representation I
learned in school. Which was not THE floating point system, it was only A
floating point system. Since seeing some of the earlier posts I have looked
into IEEE 754 and it *is* quite different - and an improvement. Bystanders
should be aware, however, that there is no assurance that C uses IEEE 754,
so coders still have to follow the old rules if they want to permit cross
platform use. That is, don't use == if you want portability. The
representation I was speaking about is still permissible.

No, it's not. Although a C implementation is not required to use IEEE 754,
it cannot use a system where 1 is not exactly representable as a
floating-point number. Such a system could not meet the requirements of the
floating-point model described in the standard. (As long as we're talking
C99; I don't know what C89 said.) I verified this with the regulars of
comp.std.c to be sure; the thread is at
http://groups.google.com/group/comp....a116e9c14faab5

The "don't use ==" exhortation applies to all floating-point systems, IEEE
754 included. This is not a portability issue, since it's not even
guaranteed (or rather supposed) to work on one platform. If you're talking
binary representations, then those are indeed not portable, but == has
nothing to do with that.

You're sure you're not confusing all this with fixed-point arithmetic? In
fixed-point it's possible for 1.0 not to be exactly representable. (Standard
C does not define any fixed-point types, but it's not hard to implement them.)

S.
May 15 '06 #12

osmium wrote:
"Vladimir Oka" writes:
I > think (before looking into the Standard) that all small integers are
guaranteed to be. Beware numbers like 1000000000000042.0000, though.


No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex


True that the value 1.0 cannot be represented exactly as a sum of the
series 1/2^n (n = -1, -2, -3, ...) with a finite number of terms.
However, since most floating point formats I'm aware of also use an
exponent, it *can* be represented exactly as

0.5 * 2^1

If you're talking about 0.1, now *that's* a problem, even with the
exponent.

May 15 '06 #13
In article <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
"osmium" <r1********@comcast.net> writes:
"Joe Wright" writes:
osmium wrote:
No. For example, the number 1 can not be represented exactly. Select as
many terms as you wish from the following series:
1/2, 1/4, 1/8, ....1/2^googolplex
.... You're right. I was referring to the floating point representation I
learned in school.


And how does this system you learned in school represent 2.0?


It looks more like fixed point than floating point to me.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
May 15 '06 #14

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

Similar topics

22
by: Allen Thompson | last post by:
Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
10
by: joshb | last post by:
Hey guys, I have a length converter which converts mm, cm, m, km, ft, yd, in and mi. If I convert 5mm to miles, I get 0.00000031068559649... miles. I'm looking to convert all my values to two...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
1
by: tom | last post by:
Hi! I'm wondering whether there's an easy way to remove unnecessary leading zeros from my floating point number. realS = float(-1.25e-5) imgS = float(-7.6e4) print complex(realS, imgS) I...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
13
by: helen.m.flynn | last post by:
Hi I'm very much a beginner with Python. I want to write a function to convert celcius to fahrenheit like this one: def celciusToFahrenheit(tc): tf = (9/5)*tc+32 return tf I want the answer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.