473,890 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Roun d % .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
13 15664
"osmium" <r1********@com cast.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_Keit h) 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 100000000000004 2.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.or g> Keith Thompson <ks***@mib.or g> writes:
"osmium" <r1********@com cast.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
2249
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
1812
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 decimal spots, but if it comes up zeros, I would like to round to two spots after the last zero. I've looked through google and haven't found anything I could use yet, so I was wondering if you guys had any scripts or any leads I could use to make...
11
6671
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 seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
29
3206
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
6202
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
1
2428
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 would like it to look like (-1.25e-5-76000j)
206
13418
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) It then updates the value with numberOfPrecisions after the decimal
20
5037
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
2467
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 correct to one decimal place, so celciusToFahrenheit(12) would return 53.6.
0
9979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10829
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.