• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What does this algorithm do?

Caveman

Platinum Member
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
 
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.
 
~= 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)).
 
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?
 
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.
 
Back
Top