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

inaccurate floating point and DBL_MAX

About inaccurate floating point and DBL_MAX.

double x = 2;
double y = 1 + 1;
assert(x == y);

Because of inaccurate floating point representation, x and y may not be
equal (ie. they may differ by 0.0000000001 or some small number) on some
implementations.

But how about DBL_MAX?

double x = DBL_MAX;
f(x);
assert(x != DBL_MAX);

Is this above comparison exact, or should we say assert(x < DBL_MAX).

Thanks.
Jul 23 '05 #1
2 4444
Siemel Naran wrote:
About inaccurate floating point and DBL_MAX.

double x = 2;
double y = 1 + 1;
assert(x == y);

Because of inaccurate floating point representation, x and y may not be
equal (ie. they may differ by 0.0000000001 or some small number) on some
implementations.
Actually, I've never seen such an implementation. The standard says that
if you have values that can not be precisely represented in a floating
variable then one of the two adjacent values is chosen. THIS IS WHERE
THE IMPRECISION COMES FROM along with the assumption that non-repreating
decimal fractions are non-repeating binary fractions as well.

I've never seen an implementation that can't precisely represent 1 and 2.

But how about DBL_MAX?

double x = DBL_MAX;
f(x);
assert(x != DBL_MAX);


Since DBL_MAX is of type double, it's already got a double representation.
There's no imprecision. You can test it for equality (provided you haven't
converted it to some other type or done calculations with it).
Jul 23 '05 #2
Siemel Naran wrote:
About inaccurate floating point and DBL_MAX.

double x = 2;
double y = 1 + 1;
assert(x == y);

Because of inaccurate floating point representation, x and y may not
be equal (ie. they may differ by 0.0000000001 or some small number)
on some implementations.
This is incorrect for a couple of reasons -- first of all, '1+1' is an
integer expression so it is done as an integer computation, then the
result is converted to a double. IOW, in both cases, you're creating 2
(as an integer) and then converting that integer to a double. The
result is clearly the same in both cases.

Even if you changed it to something like:

double x = 2.0;
dobule y = 1.0 + 1.0;
assert (x==y);

the assertion still can't fail on any conforming implementation of C++.
Even for a floating point type, there is a range of integers that must
be represented exactly, and 1 and 2 fall (well) inside of that range.
But how about DBL_MAX?

double x = DBL_MAX;
f(x);
assert(x != DBL_MAX);

Is this above comparison exact, or should we say assert(x < DBL_MAX).


DBL_MAX is (by definition) the largest possible double. That means
x!=DBL_MAX and x<DBL_MAX mean exactly the same thing unless you want to
deal with infinity, NaNs, etc. As far as precision goes, however,
DBL_MAX is already a double value, so it's not rounded during
assignment to a double.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #3

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

Similar topics

3
by: C. Sengstock | last post by:
Hi, i´ve got an iteration with random generated floating point numbers. During the iteration process sometimes the programm stops with the error: "floating point error: overflow" Is the...
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...
7
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
13
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero,...
9
by: Klaus Bonadt | last post by:
I have found strange behaviour in casting floating point values in C++ 6.0 to int: If I enter in the watch window while debugging in version 6.0 the following term: (1.4 - 1.0) * 10.0 the...
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...
17
by: spooler123 | last post by:
Just a small little program. Can not figure out what am I doing wrong. #include <stdio.h> #include <limits.h> #include <float.h> int main() { double max = FLT_MAX;
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...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
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.