Multiply help please
Zap-o
Posts: 452
I am trying to multiply a number ranging from 0 to 65535 by 100_000 and its not clear why it wont calculate this. I see in the manual that the ** operator is what I need to use but how?
Pub calculate | Temp Temp := 65535 ' or any real number from 0 up to 65535 Temp **= 100_000 debug (Temp)
Comments
65535 * 100000 = 6553500000 which is beyond the range of a long variable.
But you don't really need that. Just multiply by 10_000 and divide by 1317 using regular 32-bit Spin operations. BTW, you can't add 273.15 the way you have, since it's not an integer.
-Phil
-Phil
Y = X * 7.59301 - 27315
where X ranges from 0 to 65535. Another way to approach problems of this sort will employ the ** operator in its role as a fractional multiplier. Break the problem down into its integer and fractional parts. In Prop code, it becomes,
Y := (X * 7) + (X * 2 ** 1273479278) - 27315
In this case the fractional part is 0.59301. The ** operator approximates that (quite accurately) as the numerator of a fraction that has a denominator of 2^32:
X * 0.59301 = X * 2 * 0.296505 = X * 2 * (1273479278 / 4294967296)
The division by 2^32 is implied in the ** operator.
The reason for the factor of two is to avoid a sign bit, which would spoil it as a fractional multiply.
Y := X * 100 ** 326117296 - 27315
That is the same logic, but the original formula recast to make it a one-step fractional multiply by a factor less than 0.5...
Y = X * 7.59301 - 27315
.....= X * 100 * 0.0759301 - 27315
where 0.0759301 = 326117296 / 2^32