473,385 Members | 1,536 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,385 software developers and data experts.

fractions and floating point

Hi,

is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the opposite order?

The idea is that the two fractions are almost identical and that the
error introduced by going to floating point representation is bigger
than the exact difference, but different for the two fractions such that
it somehow turns them around.

I tried some numbers, but so far it always was ok.

I know that this depends on the way floating points are represented, so
the more precise question would be: is there a standard that would
ensure that this never happens? Does somebody have a concrete example
for a specific platform?

Thanks
Steffen


Nov 15 '05 #1
5 2385

"Steffen" <s.*********@gmx.de> wrote in message
news:3j************@news.dfncis.de...
Hi,

is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the opposite order?

The idea is that the two fractions are almost identical and that the error
introduced by going to floating point representation is bigger than the
exact difference, but different for the two fractions such that it somehow
turns them around.

I tried some numbers, but so far it always was ok.

I know that this depends on the way floating points are represented, so
the more precise question would be: is there a standard that would ensure
that this never happens? Does somebody have a concrete example for a
specific platform?

Thanks
Steffen


It can't happen, if you think about it, the worst that can come out of this
is that the numbers come out equal but if you have x=a/b, and y=c/d, and
mathematically x<y then you cannot get x>y but you may get x==y.

Allan
Nov 15 '05 #2
In article <3j************@news.dfncis.de> Steffen <s.*********@gmx.de> writes:
Hi,

is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the opposite order? .... I know that this depends on the way floating points are represented, so
the more precise question would be: is there a standard that would
ensure that this never happens? Does somebody have a concrete example
for a specific platform?


The IEEE floating point standard guarantees it. Both a/b and c/d should
be represented as the nearest floating point number, with determined
logic when there is a tie. So while you can have a/b == c/d in
floating point you will not get that a/b > c/d.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #3
On Thu, 14 Jul 2005 11:32:22 +0200, Steffen wrote:
Hi,

is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the opposite order?
They don't have to be integers, but we'll need to assume that we are
talking about values that are exactly representable in the floating point
type (i.e. there is no approximation step before the division takes
place). This isn't the case for all integers in the range of representable
values of a floating point type.
The idea is that the two fractions are almost identical and that the
error introduced by going to floating point representation is bigger
than the exact difference, but different for the two fractions such that
it somehow turns them around.
The property you want isn't guaranteed by C, but it may well be guaranteed
by IEEE 754 floating point arithmetic. It does follow when inexact
results are always rounded to the nearest representable floating point
value ***when using a consistent precision***. However C allows
intermediate results to be evaluated at a higher precision than the
underlying type represents, and this doesn't have to be consistent. So a/b
could in theory be evaluated at a different precision to c/d, and the
differing rounding that results could cause the ordering to be broken.
I tried some numbers, but so far it always was ok.

I know that this depends on the way floating points are represented, so
the more precise question would be: is there a standard that would
ensure that this never happens? Does somebody have a concrete example
for a specific platform?


The trouble is that this is VERY dependent on the architecture and the
specific code that the compiler generates. For example on x86 systems
FPU registers are 80 bits (although this is settable) and float and double
values tend to be calcuated and held at a higher precision in registers
than when they are stored in memory. So if the compiler decides to store
one intermediate result in memory but not the other the problem can occur.

Lawrence
Nov 15 '05 #4
Steffen wrote:
is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the opposite order?
Well, a/b > c/d could not be the calculated result (since rounding is
consistent). However, obviously massive underflow can cause a/b == c/d
(==0). Also if one of your calculations becomes a NaN or both become
inf, then this may change the sense of the comparison in ways you
didn't intend.
The idea is that the two fractions are almost identical and that the
error introduced by going to floating point representation is bigger
than the exact difference, but different for the two fractions such that
it somehow turns them around.

I tried some numbers, but so far it always was ok.

I know that this depends on the way floating points are represented, so
the more precise question would be: is there a standard that would
ensure that this never happens? Does somebody have a concrete example
for a specific platform?


IEEE 754 is the only seriously recognized floating point specification.
More that likely your system uses and supports it.

--
Paul Hsieh
http://www.pobox.com/~qed
http://bstring.sf.net/

Nov 15 '05 #5
In article <3j************@news.dfncis.de>,
Steffen <s.*********@gmx.de> wrote:
Hi,

is it possible to have two fractions, which (mathematically) have the
order a/b < c/d (a,b,c,d integers), but when (correctly) converted into
floating point representation just have the opposite order?


It depends on the quality of your arithmetic.

Using IEEE754 arithmetic, the result of an operation is the
mathematically exact result, correctly rounded. So if a/b < c/d
mathematically, and a, b, c, d can be converted to double without
rounding error (no 64 bit numbers), then (double) a / (double) b <=
(double) c / (double) d.

BUT it is allowed that an implementation sometimes uses more precision
than required. And then things go wrong. For example, many Intel
processors can use 64 bit and 80 bit floating point numbers. It can
happen that a/b is rounded to 64 bits, and c/d is rounded to 80 bits. In
that case a/b might be rounded up, while c/d is rounded down, so the
result of a/b might be greater than the result of c/d.
Nov 15 '05 #6

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

Similar topics

33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
7
by: KimD | last post by:
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc. C# needs explicit statement of numerator and denominator as float/double etc. e.g. 4d/3d or 4.0/3.0 to be interpreted...
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...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
15
by: farah727rash | last post by:
Hi everyone, I have this problem and I don't know what's wrong with my program. I am trying to enter my two variables height and weight as fraction numbers. I declared them as float and also as...
6
by: Cezary Noweta | last post by:
Hello, In Control Panel, Regional Settings, set decimal point to something other then dot ,,.'' (ASCII 0x2E). Let it be comma (ASCII 0x2C) for example. Compile and run the following code ===...
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...
0
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.