Representation of 1 + 2.0^-24 - numerical-methods

Im trying to figure why the number 1 + 2.0^-24 in single arithmetic precision is stored as 1.0 instead of 1 + 2^-23 since we can check that 1 + 2.0^-24 - 1 + 2^-23 = 2^-24 and 1.0 + 2.0^-24 - 1.0 = 2^-24 and 1.0 and 1.0 + 2^-23 are two closet numbers to 1.0+2^-24. Why is the computer choosing 1.0 ?

Related

getting Sum Floor and Group by to work

Ok all i have a question about sum and group by what i have currently is a error and i don't know how to fix basically the error is in regards to this line of code
SELECT PlayerName
,floor(SUM(PlayerScore / 500)) + floor(SUM(PPlayerScore / 1000)) + floor(SUM(S4 / 5)) * 2 + floor(Sum(P7 / 3)) * 2 + SUM(prereg) + SUM(st) + SUM(hb) AS BB
,floor(SUM(PlayerScore / 500)) + floor(SUM(PPlayerScore / 1000)) + floor(SUM(S4 / 5)) * 2 + floor(Sum(P7 / 3)) * 2 + SUM(PlayerBallots) + SUM(prereg) + SUM(st) + SUM(hb) AS TB
,SUM(PlayerBallots) AS PB
FROM player
the same issue is throughout the line and it is this
from this floor sum command
if 4 players have scores of 1100 1300 1800 and 1000 the sum of that will show 5 when the reality is it should only show 4 i tried adding in a group by operator it will only show the total for 1 player
is it possible to do what I'm trying or not
thank you in advance
To help a little i will shorten down the code and explain what i need it to do with an example
floor(SUM(PPlayerScore /1000))
so for each 1000 points a player scores he gets 1 ballot
so if
jane has 1100 gets 1 ballot floor 1.1
joe gets 1300 gets 1 ballot floor of 1.3
jerry gets 1800 1 ballot floor of 1.8
jane gets 1000 gets 1 ballot floor of 1
the issue is with current code i get 5 when in reality there is only 4 ballots

Horners Scheme Binary to Decimal Conversion

Using Horner's scheme, represent (evaluate) the binary unsigned whole number 11001101 in decimal. I got 410 I just want to make sure its right.
11001101
Horner's Scheme:
((((((((1*2 + 1)2 + 0)2 + 0)2 + 1)2 + 1)2 + 0)2 + 1
1*2=2+1=3*2=6+0=6*2=12+0=12*2=24+1=25*2=50+1=51*2=102+0=102*2=204+1=205
11001101 (binary) = 205 (decimal)
NOTE: You do not multiply the last 1 because the 2 exponent is actually 0 there, and 2^0=1.

Translation from Binary to Decimal

How does one translate the following binary to Decimal. And yes the decimal points are with the whole binary value
1) 101.011
b) .111
Each 1 corresponds to a power of 2, the power used is based on the placement of the 1:
101.011
= 1*2^2 + 0*2^1 + 1*2^0 + 0*2^-1 + 1*2^-2 + 2*2^-3
= 1*4 + 1*1 + 1/4 + 1/8
= 5.375
.111
= 1*2^-1 + 1*2^-2 + 1*2^-3
= 1/2 + 1/4 + 1/8
= .875
If you don't like dealing with the decimal point you can always left shift by multiplying by a power of 2:
101.011 * 2^3 = 101011
Then convert that to decimal and, since you multiplied by 2^3 = 8, divide the result by 8 to get your answer. 101011 converts to 43 and 43/8 = 5.375.
1) 101.011
= 2*2^-3 + 1*2^-2 + 0*2^-1 + 1*2^0 + 0*2^1 + 1*2^2
= (1/8) + (1/4) + 0 + 1 + 0 + 4
= 5.375
2) .111
= 1*2^-3 + 1*2^-2 + 1*2^-1
= (1/8) + (1/4) + (1/2)
= .875
101.011 should be converted like below
(101) base2 = (2^0 + 2^2) = (1 + 4) = (5) base10
(.011) base2 = 0/2 + 1/4 + 1/8 = 3/8
So in total the decimal conversion would be
5 3/8 = 5.375
Decimal numbers cannot be represented in binary. It has to be whole numbers.
Here is a simple system
Let's take your binary number for example.
101011
Every position represents a power of 2. With the left-most position representing the highest power of 2s. To visualize this, we can do the following.
1 0 1 0 1 1
2 ^ 6 2 ^ 5 2 ^ 4 2 ^ 3 2 ^ 2 2 ^ 1
We go by each position and do this math
1 * (2 ^6 ) + 0 * (2 ^ 5) + 1 * (2 ^ 4) + 0 * (2 ^ 3) + 1 * (2 ^ 2) + 1 * (2 ^ 1)
Doing the math gives us
(1 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (1 * 2) =
64 + 0 + 16 + 0 + 4 + 2 = 86
We get an answer of 86 this way.

SML - how to return a real from a function with integers?

I have the following functions:
fun power(0,n):int = 1 | power(k,n):int = n*power((k-1),n)
fun myfunction(1,n) = 1 | myfunction(2,n) = 1
| myfunction(x,1) = 1 | myfunction(x:int,n:int) = (1 div power(n,(x-1))) + myfunction(x,n- 1)
What I need to do, is return a real value from myfunction, and not an integer. I tried specifying the return value (myfunction(x:int,n:int):real), I have tried using / instead of div, and also tried changing 1's to 1.0 but nothing seems to work.
Any ideas how I should return a real value? (x and n must be integers)
You also need to convert the result of the call to power to real:
fun myfunction(1, n) = 1.0
| myfunction(2, n) = 1.0
| myfunction(x, 1) = 1.0
| myfunction(x, n) = 1.0 / real(power(n, x - 1)) + myfunction(x, n - 1)
You need to convert an int to a real using Real.fromInt. Also, use / instead of div and return 1.0 instead of 1 in the cases where you do it.

Binary calculation

I need help to calculate Processor Affinity value
0 (0000) Not allowed (that would mean use no processors)
1 (0001) Use processor 1
2 (0010) Use processor 2
3 (0011) Use both processors 1 and 2
4 (0100) Use processor 3
5 (0101) Use both processors 1 and 3
6 (0110) Use both processors 2 and 3
7 (0111) Use processors 1,2 and 3
8 (1000) Use processor 4
With 1, 2, 3 and result is 7. I wonder what formula is?
It seems to be a simple 4-digit binary number.
A 1 at the right-most position means 1, a 1 at the second position from the right means 2, at the 3rd it means 4 and at the 4th position from right (i.e. the first digit from left) it means 8. The total value is simply the sum of all those positions.
The basic idea (in pseudo-code, because we can't format formulas correctly here is):
totalValue
for every digit at position i (counted from the right, starting with 0)
totalValue = totalValue + 2^i*(digit at position i)
For example 3 (0011) the value is 0x2^3 + 0x2^2 + 1*2^1 + 1*2^0 = 0 + 0 + 2 + 1 = 3
For example 4 (0100) the value is 0x2^3 + 1x2^2 + 0*2^1 + 0*2^0 = 0 + 4 + 0 + 0 = 4
Processor_Affinity := Use_processor_1 + Use_processor_2 + Use_processor_3
So 1010; interpret it as:
0 at 1st position as OFF
1 at 2nd position as ON
0 at 3rd position as OFF
1 at 4th position as ON