home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / linux / 20290 < prev    next >
Encoding:
Text File  |  1992-12-14  |  3.2 KB  |  100 lines

  1. Newsgroups: comp.os.linux
  2. Path: sparky!uunet!munnari.oz.au!bruce.cs.monash.edu.au!monu6!vaxc.cc.monash.edu.au!apm233m
  3. From: apm233m@vaxc.cc.monash.edu.au
  4. Subject: Re: Not quite bug in math stuff
  5. Message-ID: <1992Dec14.173157.1@vaxc.cc.monash.edu.au>
  6. Lines: 88
  7. Sender: news@monu6.cc.monash.edu.au (Usenet system)
  8. Organization: Computer Centre, Monash University, Australia
  9. References: <1992Dec13.032953.4975@tc.cornell.edu>
  10. Date: Mon, 14 Dec 1992 06:31:57 GMT
  11.  
  12. In article <1992Dec13.032953.4975@tc.cornell.edu>, kutcha@eos.acm.rpi.edu (Phillip Rzewski) writes:
  13. >     I don't do much stuff with math under Linux, so I never ran into this
  14. > before. I'm sure someone else has come across this, it's probably quite
  15. > documented somewhere. But it took me rather by surprise.
  16. >     Consider the program:
  17. > #include <stdio.h>
  18. > #include <math.h>
  19. > int main(void)
  20. > {
  21. >   double d;
  22. >   long l;
  23. >   d = pow(2.0, 1.0);
  24. >   printf("%1.32f\n", d);
  25. >   l = (long) d;
  26. >   printf("%d\n", l);
  27. >   return 0;
  28. > }
  29. >     And of course the output:
  30. > 1.99999999999999977795539507496869
  31. > 1
  32. >     Now, I understand how keen it is that math software speeds things up
  33. > by using logarithms, giving us results like this. However, I know that there
  34. > are systems out there where one doesn't notice it quite so much. The
  35. > program I was originally working with on Linux (not like the one above :) )
  36. > was originally something I wrote under VAX C on a VMS system, and I got
  37. > just what I thought in the long (in other words, 2).
  38. >     Of course, I was able to fix this by simply adding 0.5 to the double
  39. > and then casting it to long. It just seems kinda icky to have to do it that
  40. > way. I mean, this means that every person who tries to do something like I
  41. > did above (which I would think is quite a few) would have to have this same
  42. > weird thing happen to them and learn how to correct it.
  43. >     I'm not one to make demands or anything, but the math people might
  44. > consider fixing this?
  45.  
  46. What version of the kernel and libraries are you using?
  47. Using the FPU emulator with 0.98.6 (I don't have a co-processor), I get
  48. 2.00000000000000000000000 [etc]
  49. with your program.
  50.  
  51. Perhaps you are using the obsolete 'soft' math libs.
  52.  
  53. Your question does raise a valid point though. It would certainly be
  54. nice if the most accurate answer were always given for floating point
  55. problems. Alas, this is simply not possible. The use of finite precision
  56. arithmetic should always be accompanied by an appropriate degree of care.
  57. Linux is often not bad for simple problems because it runs the co-processor
  58. at ~64 bit precision (doubles are ~53 bit precision under Linux gcc).
  59.  
  60. However, even simple programs can give results which are worse than you
  61. might expect. Consider the following little program:
  62.  
  63. #include <stdio.h>
  64. void main(void)
  65. { double d;
  66.   d = 1.0; d = d/3.0;
  67.   printf("%g\n", 1.0-d*3.0); }
  68.  
  69. This will give a non-zero result (compile with no optimizations).
  70.  
  71. But if you try:
  72.  
  73. #include <stdio.h>
  74. void main(void)
  75. { double d, e;
  76.   d = 1.0; e = 3.0;
  77.   printf("%g\n", 1.0-d/3.0*e); }
  78.  
  79. Then because the intermediate result 0.333333... is kept on the co-
  80. processor stack in this case, a different result is obtained (it
  81. happens to be zero).
  82.  
  83. -- 
  84. ---------------
  85. Bill Metzenthen
  86. Mathematics Department
  87. Monash University
  88. Australia
  89.