473,623 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linux rounding error???

Sorry if I got the wrong group.

I seem to be running into a rounding issue on a Linux AS2.1 box using
gcc 2.95. This program always returns 419 on Linux, but on a Solaris
machine, I get the expected 420 output.

------------------------------------------------
#include <math.h>
#include <stdio.h>

int main()
{
double x = 0.0010;
double y = 0.420;
int result = 0;

result = (int)(y/x);
printf("y/x is %d\n",result);
return 0;
}
------------------------------------------------

Anybody know why?

Nov 14 '05 #1
4 2008
Kelly Mah wrote:
Sorry if I got the wrong group.

I seem to be running into a rounding issue on a Linux AS2.1 box using
gcc 2.95. This program always returns 419 on Linux, but on a Solaris
machine, I get the expected 420 output.

------------------------------------------------
#include <math.h> There is no point in including headers you don't use.
#include <stdio.h>

int main()
{
double x = 0.0010;
double y = 0.420;
int result = 0;

result = (int)(y/x);
printf("y/x is %d\n",result);
return 0;
}
------------------------------------------------

Anybody know why?


This is yet another variation on the never-ending question: "I don't
understand floating-point arithmetic and am too lazy to check the FAQ.
Why don't floating-point numbers have infinit precision?" Try the
following on your computer:

#include <stdio.h>
#include <float.h>

int main(void)
{
double x = 0.0010;
double y = 0.420;
long double xl = 0.0010L;
long double yl = 0.420L;
float xf = 0.0010f;
float yf = 0.420f;

printf("In all cases, bogus precision is printed;\n"
"xxx_DIG + 4 is used rather than xxx_DIG\n\n");
printf("y/x = %.*g/%.*g = %.*g, (int)(y/x)= %d\n", DBL_DIG + 4, y,
DBL_DIG + 4, x, DBL_DIG + 4, y / x, (int) (y / x));
printf("yl/xl = %.*Lg/%.*Lg = %.*Lg, (int)(yl/xl)= %d\n",
LDBL_DIG + 4, yl, LDBL_DIG + 4, xl, LDBL_DIG + 4, yl / xl,
(int) (yl / xl));
printf("yf/xf = %.*g/%.*g = %.*g, (int)(yf/xf)= %d\n", FLT_DIG + 4,
yf, FLT_DIG + 4, xf, FLT_DIG + 4, yf / xf, (int) (yf / xf));
return 0;
}

In all cases, bogus precision is printed;
xxx_DIG + 4 is used rather than xxx_DIG

y/x = 0.4199999999999 999845/0.0010000000000 00000021 = 420, (int)(y/x)= 420
yl/xl = 0.4199999999999 999999827/0.0009999999999 999999999133 = 420,
(int)(yl/xl)= 420
yf/xf = 0.4199999869/0.001000000047 = 419.9999695, (int)(yf/xf)= 419

Nov 14 '05 #2
Kelly Mah wrote:

Sorry if I got the wrong group.

I seem to be running into a rounding issue on a Linux AS2.1 box using
gcc 2.95. This program always returns 419 on Linux, but on a Solaris
machine, I get the expected 420 output.

------------------------------------------------
#include <math.h>
#include <stdio.h>

int main()
{
double x = 0.0010;
double y = 0.420;
int result = 0;

result = (int)(y/x);
printf("y/x is %d\n",result);
return 0;
}
------------------------------------------------

Anybody know why?


Let's assume (for the purposes of illustration) that Platform X has
16 bits of precision below the point. On Platform X, the closest we
can get to 0.001 is 0.0010070800781 25 or so. In binary, this is
0.0000000001000 010. The closest we can get to 0.42 on Platform X is
0.4199981689453 125 (in binary, that is 0.0110101110000 101).

Dividing one by the other, we get 417.04545... which is considerably
less accurate than your own findings on your platforms. Now, we can
get better precision by using more bits below the point. How much
better can we get? Obviously, it depends on how many bits we use.

And there's your explanation. :-)
Nov 14 '05 #3
Kelly Mah wrote:

I seem to be running into a rounding issue on a Linux AS2.1 box
using gcc 2.95. This program always returns 419 on Linux, but on
a Solaris machine, I get the expected 420 output.

------------------------------------------------
#include <math.h>
#include <stdio.h>

int main()
{
double x = 0.0010;
double y = 0.420;
int result = 0;

result = (int)(y/x);
printf("y/x is %d\n",result);
return 0;
}
------------------------------------------------

Anybody know why?


Yes. The line "result = y/x" computes 419.99999... (the cast is
pointless, and like most casts should not be present) and the
conversion to int on assignment properly truncates. Or the line
computes 420.00000000000 0000001, etc. The point is floating point
arithmetic is not exact.

You can get more consistent results by controlling rounding:

result = y/x + 0.5;

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #4
Kelly Mah wrote:
Sorry if I got the wrong group.

I seem to be running into a rounding issue on a Linux AS2.1 box using
gcc 2.95. This program always returns 419 on Linux, but on a Solaris
machine, I get the expected 420 output.

------------------------------------------------
#include <math.h>
#include <stdio.h>

int main()
{
double x = 0.0010;
double y = 0.420;
int result = 0;

result = (int)(y/x);
printf("y/x is %d\n",result);
return 0;
}
------------------------------------------------

Anybody know why?


I see division, not rounding.
Learn the difference.

gtoomey
Nov 14 '05 #5

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

Similar topics

20
11701
by: Raoul Watson | last post by:
By any chance, anyone got a rounding routine that does a work around the VB "round" bug? I still find it amazing that a company as large as Microsoft would put out a math package that is so problematic.
34
2682
by: Keith S. | last post by:
Hi Folks, When converting a double to an int, the result is not as I'd expect on Linux: #include <stdio.h> int main(int argc, char **argv) { double val = 0.24;
2
3737
by: alok | last post by:
I am getting inconsistent behvior on Linux and Solaris platfors while computing doule ( 64 bit precision ) multiplications. I have following two double numbers whose integer representation is as following I have a union typedef union { double double_val; unsigned long long uint_val;
12
13635
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically put the following code into your C# app: float testFloat2 = (int) (4.2f * (float)100); Console.Out.WriteLine("1: "+testFloat2); and the result will be 419
10
3175
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer = 2/3 'after assignment, x is 1, whereas a sane person would say it should be 0 Does Microsoft have a reason for this design decision? I understand that this type of rounding can reduce the overall error in long computation chains by reducing the...
1
2126
by: sconnors | last post by:
I am currently working on an application that runs on Windows XP SP2 with .NET 2.0, Linux 2.4.19-16mdkenterprise and Solaris 5.9. The language is C++. When I compare the output numbers, Windows rounds the numbers up (for example: 13030.65 becomes 13030.7 - precision is 6) and in Linux/Solaris the number is rounded down or more likely truncated (for example: 13030.65 becomes 13030.6). I can set the precision on the ostream or output file...
13
6177
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
206
13213
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
30
29537
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
8217
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
8160
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
8661
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8312
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,...
1
6104
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4067
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
4153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1467
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.