Please translate into english thank you
pilot0315
Posts: 910
PUB FSqr(singleA) : single | s, x, m, root ''Compute square root of singleA if singleA > 0 'if a =< 0, result 0 Unpack(@s, singleA) 'unpack input m >>= !x & 1 'if exponent even, shift mantissa down x ~>= 1 'get root exponent root := $4000_0000 'compute square root of mantissa repeat 31 result |= root if result ** result > m result ^= root root >>= 1 m := result >> 1 return Pack(@s) 'pack result
ModEdit: code tags added
Comments
Are you aware how 32-bit floating point numbers are packaged (stored as bits)?
The "Unpack" methods breaks the floating point number into three separate variables. "s" is the sign variable which indicates if the number is negative or not. "x" is the exponent of the floating point value and "m" is the mantissa of the floating point value.
The method ends with a call to "Pack" which takes the three separate numbers (s, x and m) and packs the bits appropriately for a 32-bit float.
The code in between "Unpack" and "Pack" is obviously some form of Voodoo enchantment.
Floating point math is pain to use. You're almost always better off using integer math. As @Mickster said, "If you can't solve the problem with integers, you don't understand the problem." Take a look at this thread for a discussion about using integers instead of floats.
@Duane Degn, there may be enough wetware functioning tonight to believe the geographic origin of the likely sorcerer and original source of the noted incantation to be closer to Old World Druid magic than the Voodoo of the New World.
@"Duane Degn"
@"frank freedman"
Looks like: C12H17N2O4P to me. It makes my serotonin receptors freak out. I will look at the @Mickster thread. Thanks for the laugh and the assist.
The loop is a bit-by-bit search of the exact square root.
Oh, and because it is for floating point, the used bits of the mantissa doesn't change. Which is why it can get away with just comparing the high 32 bits using ** operator.
@everybody
I found this in the prop tool library: decfloat.spin2. Used the power functions and in stead of two squared is 4 using the power I get 8. What am I missing?
obj
Using the Jm_fullduplexserial to print somehow is giving me an issue with the floating points. Any examples of doing plain old math out there? The simple stuff sometimes is confusing.
Thanks
FromInt(n) : converts signed integer to decimal float
You can't use integers as the input to most floating point methods. You first need to convert the number from an integer to a float.
Enter a few numbers into this website and you can get an idea of how the floats are stored.
The number 2 as an integer is binary %00000000000000000000000000000010. When 2 is stored as a 32-bit floating point number, the binary is %01000000000000000000000000000000. You can't perform integer math on floats and you can't perform floating point math with integers.
@evanh
@"Duane Degn"
@"frank freedman"
Thanks I think I got it.
Martin