What does this algorithm do?

Caveman

Platinum Member
Nov 18, 1999
2,537
34
91
I am taking a Numerical Methods class this semester and have been given the following algorithm:

****************************
power = 1.0
b = 1.0

while b ~= 0.0
power = power / 2
a = 1.0 + power
b = a - 1.0
display(power, a, b)
end

display("Unit Round = ", 2*power)
******************************

First question - the "while b~= 0.0" statement... I've never seen the "~=" sign before in an algorithm. What does it mean? Does it mean "Not equal to"? Does it mean "approximately equal to"?

I am supposed to write a program that implements this algorithm. To me, there seems to be absolutely nothing to implement!!! I can't see anything useful that this algorithm does...let alone write a program that does something with it.

Is this a well-known algorithm that I should be aware of? Does it do something useful that I am not seeing? It just seems to show that "power" gets very small...

Any help would be appreciated!

Caveman
 

parsley007

Golden Member
Aug 13, 2003
1,196
0
0
i would say from my math experience, it would be approxiately equal to. however, i don't have any experience with number algorithms, so take that with a grain of salt.
 

TuxDave

Lifer
Oct 8, 2002
10,571
3
71
lol... i see what the algorithm does. I should've looked at your class title, it brought a big clue.
 

oboeguy

Diamond Member
Dec 7, 1999
3,907
0
76
~= means "not equal to".

The "algorithm" appears to divide by two and do some stupid assignments. Note that in a theoretical sense, b = power. However, in the real world, we have roundoff error, because you only have so many digits to use in floating point arithmetic. Think about that, code it, look at the output, and you should see what it determines (for one thing, you'll see that b ~= power at termination (wo0t use that new notation)).
 

gunblade

Golden Member
Nov 18, 2002
1,470
0
71
while b ~= 0.0
power = power / 2
a = 1.0 + power
b = a - 1.0
display(power, a, b)
end

a = 1.0 + power
b = a - 1.0

wouldn't it make b = power always?
 

TuxDave

Lifer
Oct 8, 2002
10,571
3
71
Originally posted by: gunblade
while b ~= 0.0
power = power / 2
a = 1.0 + power
b = a - 1.0
display(power, a, b)
end

a = 1.0 + power
b = a - 1.0

wouldn't it make b = power always?

Only on an infinitely precise computer. It's supposed to test the floating point accuracy of a computer.