Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Floating point subtraction with FLT_MAX error

Reply
Thread Tools

Floating point subtraction with FLT_MAX error

 
 
spooler123@gmail.com
Guest
Posts: n/a
 
      10-25-2007
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;
double sub = 16703.627681;

double result = max - sub;

printf("%f - %f = %f\n", max, sub, result);

return 0;
}

Output:
340282346638528859811704183484516925440.000000 - 16703.627681 =
340282346638528859811704183484516925440.000000


Any help would be highly appreciated.

Thanks

 
Reply With Quote
 
 
 
 
husterk
Guest
Posts: n/a
 
      10-26-2007
On Oct 25, 7:56 pm, (E-Mail Removed) wrote:
> 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;
> double sub = 16703.627681;
>
> double result = max - sub;
>
> printf("%f - %f = %f\n", max, sub, result);
>
> return 0;
>
> }
>
> Output:
> 340282346638528859811704183484516925440.000000 - 16703.627681 =
> 340282346638528859811704183484516925440.000000
>
> Any help would be highly appreciated.
>
> Thanks


It appears that you are trying to print a double (%Lf) by using a
float (%f) hence the error you are seeing. Check out the link below
for more info on the printf() function and it's modifiers.

http://www.cplusplus.com/reference/c...io/printf.html

Keith
http://www.doubleblackdesign.com
http://www.doubleblackdesign.com/forums

 
Reply With Quote
 
 
 
 
spooler123@gmail.com
Guest
Posts: n/a
 
      10-26-2007
On Oct 25, 5:06 pm, husterk <(E-Mail Removed)> wrote:
> On Oct 25, 7:56 pm, (E-Mail Removed) wrote:
>
>
>
> > 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;
> > double sub = 16703.627681;

>
> > double result = max - sub;

>
> > printf("%f - %f = %f\n", max, sub, result);

>
> > return 0;

>
> > }

>
> > Output:
> > 340282346638528859811704183484516925440.000000 - 16703.627681 =
> > 340282346638528859811704183484516925440.000000

>
> > Any help would be highly appreciated.

>
> > Thanks

>
> It appears that you are trying to print a double (%Lf) by using a
> float (%f) hence the error you are seeing. Check out the link below
> for more info on the printf() function and it's modifiers.
>
> http://www.cplusplus.com/reference/c...io/printf.html
>
> Keithhttp://www.doubleblackdesign.comhttp://www.doubleblackdesign.com/forums


Tried that already. Same results.

%lf
340282346638528859811704183484516925440.000000 - 16703.627681 =
340282346638528859811704183484516925440.000000

%LF (Isn't it for long double?)
340282346638528859811704183484516925440.000000 - 16703.627681 =
0.000000


Thanks for the reply.

Also as far as I know printf does not care if it is %f or %lf as
everything goes as double, it is scanf which complains. Correct me if
I am wrong.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      10-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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;
> double sub = 16703.627681;
>
> double result = max - sub;
>
> printf("%f - %f = %f\n", max, sub, result);
>
> return 0;
> }
>
> Output:
> 340282346638528859811704183484516925440.000000 - 16703.627681 =
> 340282346638528859811704183484516925440.000000


What happens if you do:
double max = DBL_MAX;

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      10-26-2007
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:
>Just a small little program. Can not figure out what am I doing wrong.


>double max = FLT_MAX;
>double sub = 16703.627681;


>double result = max - sub;


Floating point does not have indefinite precision. What you
have discovered is that near FLT_MAX, the numbers that your
floating point system are able to represent are more than 16703 apart.

Different systems use different schemes for floating point. One
of the most common schemes is IEEE 754,
http://en.wikipedia.org/wiki/IEEE_fl...point_standard
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-26-2007
(E-Mail Removed) wrote:
> 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;
> double sub = 16703.627681;
>
> double result = max - sub;
>
> printf("%f - %f = %f\n", max, sub, result);
>
> return 0;
> }
>
> Output:
> 340282346638528859811704183484516925440.000000 - 16703.627681 =
> 340282346638528859811704183484516925440.000000
>
>
> Any help would be highly appreciated.
>
> Thanks
>

I've changed your code slightly for format considerations.

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

int main(void) {
double max = FLT_MAX;
double sub = 16703.627681;
double result = max - sub;
printf("%f - %f =\n%f\n", max, sub, result);
return 0;
}

Output:
340282346638528859820000000000000000000.000000 - 16703.627681 =
340282346638528859820000000000000000000.000000

FLT_MAX has magnitude e+38 while double has precision of 16 digits or
so. Your subtrahend (minuend?) is simply too small to make a difference.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-26-2007
On Fri, 26 Oct 2007 00:06:27 -0000, husterk <(E-Mail Removed)> wrote
in comp.lang.c:

> On Oct 25, 7:56 pm, (E-Mail Removed) wrote:
> > 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;
> > double sub = 16703.627681;
> >
> > double result = max - sub;
> >
> > printf("%f - %f = %f\n", max, sub, result);
> >
> > return 0;
> >
> > }
> >
> > Output:
> > 340282346638528859811704183484516925440.000000 - 16703.627681 =
> > 340282346638528859811704183484516925440.000000
> >
> > Any help would be highly appreciated.
> >
> > Thanks

>
> It appears that you are trying to print a double (%Lf) by using a
> float (%f) hence the error you are seeing. Check out the link below
> for more info on the printf() function and it's modifiers.


Did you write this in an absent-minded moment, or are you actually
this mistaken about printf() conversion specifiers?

> http://www.cplusplus.com/reference/c...io/printf.html


Even the page you reference states:

"L The argument is interpreted as a long double (only applies to
floating point specifiers: e, E, f, g and G)."

It is undefined behavior to pass a double to printf() with a "%Lf"
conversion specifier, although it will probably work on
implementations, like Microsoft's compilers for 32-bit Windows, where
double and long double have the same size and representation.

"%f" is, has always been, and always will be the correct conversion
specifier for double.

> Keith


Also, please set your posting software to add a proper signature
delimiter, namely "-- \n".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-26-2007
(E-Mail Removed) wrote:
> Just a small little program. Can not figure out what am I doing wrong.
>
> #include <stdio.h>
> #include <limits.h>

Note that <limits.h> is unused by your program
> #include <float.h>
>
> int main()
> {
> double max = FLT_MAX;
> double sub = 16703.627681;
> double result = max - sub;
> printf("%f - %f = %f\n", max, sub, result);
> return 0;
> }
>
> Output:
> 340282346638528859811704183484516925440.000000 - 16703.627681 =
> 340282346638528859811704183484516925440.000000
>
>
> Any help would be highly appreciated.



/* The values FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON are defined
to be, for each type, the smallest x > 0.0 such that 1.0+x > x.
These suggests (but does not guarantee) that for a value y > 0.0,
y*(1.0+x) is the smallest value larger than y. These values are
closely linked to the number of significant bits in the
representation of the type. There is a limit FLT_DIG, DBL_DIG, or
LDBL_DIG representing the guaranteed number of (decimal) digits of
precision. Check the following program. */
#include <stdio.h>
#include <float.h>
#include <math.h>

int main()
{
double max = FLT_MAX;
double sub = 16703.627681;
double diff;
printf
("The following values are all dependent on the implementation.\n"
"They may or may not be similar to the values for your "
"implementation.\n\n");

printf("FLT_DIG = %d\n", FLT_DIG);
printf("FLT_MAX = %.*g, FLT_EPSILON = %.*g,\n"
"FLT_MAX - FLT_MAX/(1.0+FLT_EPSILON) = %.*g,\n"
"log10(1.0/FLT_EPSILON - 1.0) = %.*g\n\n",
FLT_DIG, FLT_MAX,
FLT_DIG, FLT_EPSILON,
FLT_DIG, FLT_MAX - FLT_MAX / (1.0 + FLT_EPSILON),
FLT_DIG, log10(1.0 / FLT_EPSILON - 1.0));

printf("DBL_DIG = %d\n", DBL_DIG);
printf("DBL_MAX = %.*g, DBL_EPSILON = %.*g,\n"
"DBL_MAX - DBL_MAX/(1.0+DBL_EPSILON) = %.*g,\n"
"log10(1.0/DBL_EPSILON - 1.0) = %.*g\n\n",
DBL_DIG, DBL_MAX,
DBL_DIG, DBL_EPSILON,
DBL_DIG, DBL_MAX - DBL_MAX / (1.0 + DBL_EPSILON),
DBL_DIG, log10(1.0 / DBL_EPSILON - 1.0));

printf("LDBL_DIG = %d\n", LDBL_DIG);
printf("LDBL_MAX = %.*Lg, LDBL_EPSILON = %.*Lg,\n"
"LDBL_MAX - LDBL_MAX/(1.0+LDBL_EPSILON) = %.*Lg,\n"
"log10(1.0/LDBL_EPSILON - 1.0) = %.*g\n\n",
LDBL_DIG, LDBL_MAX,
LDBL_DIG, LDBL_EPSILON,
LDBL_DIG, LDBL_MAX - LDBL_MAX / (1.0 + LDBL_EPSILON),
DBL_DIG, log10(1.0 / LDBL_EPSILON - 1.0));

diff = max * (1. - 1. / (1.0 + DBL_EPSILON));
printf("max is a double with value %.*g.\n"
"We expect the next lower distinguishable double to\n"
" differ from max by about %.*g.\n"
"The original poster wanted to distinguish a value %.*g\n"
"less than max, but this value is only"
" %.*g * the (likely) smallest significant difference.\n",
DBL_DIG, max, DBL_DIG, diff, DBL_DIG, sub,
DBL_DIG, sub / diff);


return 0;
}


[Output]

The following values are all dependent on the implementation.
They may or may not be similar to the values for your implementation.

FLT_DIG = 6
FLT_MAX = 3.40282e+38, FLT_EPSILON = 1.19209e-07,
FLT_MAX - FLT_MAX/(1.0+FLT_EPSILON) = 4.05648e+31,
log10(1.0/FLT_EPSILON - 1.0) = 6.92369

DBL_DIG = 15
DBL_MAX = 1.79769313486232e+308, DBL_EPSILON = 2.22044604925031e-16,
DBL_MAX - DBL_MAX/(1.0+DBL_EPSILON) = 3.99168061906944e+292,
log10(1.0/DBL_EPSILON - 1.0) = 15.653559774527

LDBL_DIG = 18
LDBL_MAX = 1.18973149535723177e+4932, LDBL_EPSILON =
1.08420217248550443e-19,
LDBL_MAX - LDBL_MAX/(1.0+LDBL_EPSILON) = 1.28990947194073851e+4913,
log10(1.0/LDBL_EPSILON - 1.0) = 18.9648897268308

max is a double with value 3.40282346638529e+38.
We expect the next lower distinguishable double to
differ from max by about 7.55578592223147e+22.
The original poster wanted to distinguish a value 16703.627681
less than max, but this value is only 2.21070684809276e-19 * the
(likely) smallest significant difference.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-26-2007
(E-Mail Removed) wrote:
>
> #include <stdio.h>
> #include <limits.h>
> #include <float.h>
>
> int main() {
> double max = FLT_MAX;
> double sub = 16703.627681;
> double result = max - sub; /* illegal */
>
> printf("%f - %f = %f\n", max, sub, result); /* illegal */
> return 0;
> }


I reformatted your code for sanity. See the added comments. A
float is not a double. An object is not a constant.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      10-26-2007
"CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> (E-Mail Removed) wrote:
>>
>> #include <stdio.h>
>> #include <limits.h>
>> #include <float.h>
>>
>> int main() {
>> double max = FLT_MAX;
>> double sub = 16703.627681;
>> double result = max - sub; /* illegal */
>>
>> printf("%f - %f = %f\n", max, sub, result); /* illegal */
>> return 0;
>> }

>
> I reformatted your code for sanity. See the added comments. A
> float is not a double. An object is not a constant.


What is illegal on the lines you commented ?

--
Chqrlie.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subtraction of floating point numbers Jaroslav Dobrek Python 2 02-24-2012 10:16 AM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Floating point subtraction rounding error (NOT display error) Keflavich Python 13 12-14-2007 03:56 PM
Number of characters required for FLT_MAX artifact.one@googlemail.com C Programming 4 03-20-2007 09:19 AM
floating point subtraction question Nicholas Rahn Ruby 4 11-27-2005 11:39 PM



Advertisments