mersenneforum.org  

Go Back   mersenneforum.org > Fun Stuff > Puzzles

Reply
 
Thread Tools
Old 2009-11-06, 07:15   #1
lfm
 
lfm's Avatar
 
Jul 2006
Calgary

52×17 Posts
Default computer accuracy

Predict the output of this simple C program before you run it:


#include <stdio.h>

int main(void) {
double a, b, c, d;

a = 0.0;
do {
a = a + 1.0;
b = 1.0 / a;
c = b * a;
} while (c == 1.0);

printf("%g\n", a);

return 0;
}
lfm is offline   Reply With Quote
Old 2009-11-06, 07:42   #2
Batalov
 
Batalov's Avatar
 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2

2·11·457 Posts
Default

Predict it with float, too.
Batalov is offline   Reply With Quote
Old 2009-11-06, 09:13   #3
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

3·17·131 Posts
Default

I would guess: 2[sup]53[/sup]+1
retina is online now   Reply With Quote
Old 2009-11-06, 13:11   #4
Brian-E
 
Brian-E's Avatar
 
"Brian"
Jul 2007
The Netherlands

2·11·149 Posts
Default

Your puzzle makes me reflect that I worked for 8 years in a programming environment without ever once using floating point arithmetic in anything I wrote.

Still, no harm in guessing, baseless though my guess is:

Depending on the compiler, the program will either output 1.0 or not terminate. (A good compiler might combine the commands in the loop setting b and c to simply assign c=1.0.)
Plus the compiler should warn about testing the equality c==1.0 with floating point numbers, not to mention the failure to use the variable d.
Brian-E is offline   Reply With Quote
Old 2009-11-06, 14:36   #5
petrw1
1976 Toyota Corona years forever!
 
petrw1's Avatar
 
"Wayne"
Nov 2006
Saskatchewan, Canada

10100100110112 Posts
Default

I agree with the last part:

d is definded but not used
petrw1 is offline   Reply With Quote
Old 2009-11-06, 14:42   #6
WMHalsdorf
 
WMHalsdorf's Avatar
 
Feb 2005
Bristol, CT

33×19 Posts
Default

It's a question of precision.
a=3 since b = 1/3 can't be represented precisely in floating point.
WMHalsdorf is offline   Reply With Quote
Old 2009-11-06, 20:13   #7
storm5510
Random Account
 
storm5510's Avatar
 
Aug 2009
Not U. + S.A.

32·281 Posts
Default

I doesn't do anything but loop repeatedly. C is always equal to 1.
storm5510 is offline   Reply With Quote
Old 2009-11-06, 20:38   #8
ewmayer
2ω=0
 
ewmayer's Avatar
 
Sep 2002
República de California

2DEB16 Posts
Default

I guessed 3, but in the real world I see something rather different:

gcc -O0 gives 49

gcc -O3 gives 41

Interesting...

Last fiddled with by ewmayer on 2009-11-06 at 20:38
ewmayer is offline   Reply With Quote
Old 2009-11-06, 21:53   #9
flouran
 
flouran's Avatar
 
Dec 2008

72·17 Posts
Default

I'm a beginner programmer, but isn't this an infinite loop?
flouran is offline   Reply With Quote
Old 2009-11-06, 23:07   #10
TimSorbet
Account Deleted
 
TimSorbet's Avatar
 
"Tim Sorbera"
Aug 2006
San Antonio, TX USA

11×389 Posts
Default

Quote:
Originally Posted by flouran View Post
I'm a beginner programmer, but isn't this an infinite loop?
Only if you have no rounding errors at all. That's where the challenge comes in: exactly when would the given language (C) using the given method for storing fractional parts (the one specified by double) make a rounding error big enough to cause the condition (c == 1.0) to fail?

Last fiddled with by TimSorbet on 2009-11-06 at 23:11
TimSorbet is offline   Reply With Quote
Old 2009-11-07, 00:32   #11
Batalov
 
Batalov's Avatar
 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2

274616 Posts
Default

It doesn't really have to do with the language (except for the bit that the compiler could optimize the whole thing out). It's the IEEE standard for , and it actually tries really hard to do the right thing, and that's why the loop goes as far as it does, which is fairly amazing.

Here's a document that indeed every computer scientist... etc.
Written more than 15 years ago. Great reading.
Batalov is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accuracy and Precision davieddy Math 0 2011-03-14 22:54
CPU Credit Accuracy g0vegan PrimeNet 1 2008-11-04 20:26
Verify Accuracy of Test Numbers PrimeNet 8 2005-07-31 08:16
Calculating sieving % accuracy amcfarlane Math 3 2005-01-02 19:34
Accuracy of our work [k*2^n-1, k<300] Kosmaj 15k Search 87 2004-11-13 09:35

All times are UTC. The time now is 23:18.


Wed Feb 1 23:18:35 UTC 2023 up 167 days, 20:47, 0 users, load averages: 0.96, 0.90, 0.90

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.

≠ ± ∓ ÷ × · − √ ‰ ⊗ ⊕ ⊖ ⊘ ⊙ ≤ ≥ ≦ ≧ ≨ ≩ ≺ ≻ ≼ ≽ ⊏ ⊐ ⊑ ⊒ ² ³ °
∠ ∟ ° ≅ ~ ‖ ⟂ ⫛
≡ ≜ ≈ ∝ ∞ ≪ ≫ ⌊⌋ ⌈⌉ ∘ ∏ ∐ ∑ ∧ ∨ ∩ ∪ ⨀ ⊕ ⊗ 𝖕 𝖖 𝖗 ⊲ ⊳
∅ ∖ ∁ ↦ ↣ ∩ ∪ ⊆ ⊂ ⊄ ⊊ ⊇ ⊃ ⊅ ⊋ ⊖ ∈ ∉ ∋ ∌ ℕ ℤ ℚ ℝ ℂ ℵ ℶ ℷ ℸ 𝓟
¬ ∨ ∧ ⊕ → ← ⇒ ⇐ ⇔ ∀ ∃ ∄ ∴ ∵ ⊤ ⊥ ⊢ ⊨ ⫤ ⊣ … ⋯ ⋮ ⋰ ⋱
∫ ∬ ∭ ∮ ∯ ∰ ∇ ∆ δ ∂ ℱ ℒ ℓ
𝛢𝛼 𝛣𝛽 𝛤𝛾 𝛥𝛿 𝛦𝜀𝜖 𝛧𝜁 𝛨𝜂 𝛩𝜃𝜗 𝛪𝜄 𝛫𝜅 𝛬𝜆 𝛭𝜇 𝛮𝜈 𝛯𝜉 𝛰𝜊 𝛱𝜋 𝛲𝜌 𝛴𝜎𝜍 𝛵𝜏 𝛶𝜐 𝛷𝜙𝜑 𝛸𝜒 𝛹𝜓 𝛺𝜔