Whole numbers and fractions
Three players found a chest with 100 diamonds. How many does each get, and how many are left? How many items do your chests hold on average? Integers answer the first question, FLOATs the second – and turning one into the other is where the surprises are.
Functions: DIV, ROUND, TO_FLOAT, INT and FIX
What you need
A trigger block with a button and a display block. For share, the variable items items and players of type INTEGER and two uninitialized items; for average, a variable item counts of type ARRAY INTEGER and two uninitialized items; for compare, a variable item value of type FLOAT and three uninitialized items.
The idea
c0de has two kinds of numbers. An INTEGER counts things that can't be split, like diamonds. A FLOAT measures things that can, like an average. The operator / always gives a FLOAT: 100 / 3 is 33.333…. For sharing items you need the whole-number answer instead, and that is DIV: 100 DIV 3 is 33. The operator \ gives the remainder, 1.
Going the other way, a FLOAT becomes an INTEGER in one of three ways, and they only agree for positive numbers without a .5:
| Value | ROUND (nearest) | INT (down) | FIX (cut off) |
|---|---|---|---|
| 2.5 | 2 | 2 | 2 |
| -2.5 | -2 | -3 | -2 |
| -12.85 | -13 | -13 | -12 |
ROUND sends a .5 to the even neighbor, so 2.5 becomes 2 and 3.5 becomes 4. Over many numbers, that rounds up as often as down.
The program
PROGRAM fairshare
NOOP
INCLUDE c0de/math
NOOP
FUNCTION share
PARAMETER items INTEGER
PARAMETER players INTEGER
RESULT each INTEGER
RESULT rest INTEGER
BEGIN
CALL c0de/math/DIV
PARAMETER dividend items ;
PARAMETER divisor players ;
RESULT result each
ASSIGN rest items \ players ;
CALL PRINT
PARAMETER value players ⊕ " players get " ⊕ each ⊕ " each, " ⊕ rest ⊕ " left" ;
END FUNCTION
NOOP
FUNCTION average
PARAMETER counts ARRAY INTEGER
RESULT exact FLOAT
RESULT rounded INTEGER
BEGIN
VARIABLE sum INTEGER
VARIABLE size INTEGER
VARIABLE total FLOAT
CALL LEN
PARAMETER value counts ;
RESULT result size
IF size = 0 ;
BEGIN
RETURN
END IF
EACH count counts
BEGIN
ASSIGN sum sum + count ;
END EACH
CALL c0de/math/TO_FLOAT
PARAMETER value sum ;
RESULT result total
ASSIGN exact total / size ;
CALL c0de/math/ROUND
PARAMETER value exact ;
RESULT result rounded
CALL PRINT
PARAMETER value size ⊕ " chests, average " ⊕ exact ⊕ ", about " ⊕ rounded ;
END FUNCTION
NOOP
FUNCTION compare
PARAMETER value FLOAT
RESULT rounded INTEGER
RESULT down INTEGER
RESULT cut INTEGER
BEGIN
CALL c0de/math/ROUND
PARAMETER value value ;
RESULT result rounded
CALL c0de/math/INT
PARAMETER value value ;
RESULT result down
CALL c0de/math/FIX
PARAMETER value value ;
RESULT result cut
END FUNCTIONBuilding and running it
Bind the display block to the program fairshare and the function you want to try, and set the trigger block to the same function.
share: withitems= 100 andplayers= 3, the results areeach= 33 andrest= 1, and the display shows3 players get 33 each, 1 left.average: withcounts=[12, 7, 9, 3], the results areexact= 7.75 androunded= 8, and the display shows4 chests, average 7.75, about 8. An empty list gives 0.0 and 0.compare: withvalue= -12.85, the results arerounded= -13,down= -13 andcut= -12. Try 2.5 and -2.5, too.
With players = 0, share crashes with DIVISION_BY_ZERO: nobody can share with no one. The same happens with / and \.
How it works
- DIV rounds towards 0. For positive numbers, that is the number of full shares.
restis what\leaves over, soeach * players + restis alwaysitemsagain. - TO_FLOAT makes the sum a FLOAT.
sumcounts items, so it is an INTEGER. Strictly,/would turn it into a FLOAT by itself, and so would an ASSIGN to a FLOAT variable; TO_FLOAT says it explicitly. It matters where c0de doesn't convert:⊕writes the INTEGER 31 as31, the FLOAT as31.0. - The empty list. Without the IF,
total / sizewould divide by 0. RETURN leaves the function with the default values of its results, 0.0 and 0. - ROUND for "about".
roundedis the nearest whole number of items. For an average of 7.5, it would be 8, for 6.5 it would be 6 – the even neighbor.
Try it yourself
- 100 diamonds for 3 players leave 1 over. Give the rest to the first players, one each, so nothing is left: 34, 33 and 33.
Solution
A FOR loop gives everybody each, and the first rest players one more. With 30 items for 4 players, shares is [8, 8, 7, 7].
PROGRAM fairsplit
NOOP
INCLUDE c0de/math
NOOP
FUNCTION split
PARAMETER items INTEGER
PARAMETER players INTEGER
RESULT shares ARRAY INTEGER
BEGIN
VARIABLE each INTEGER
VARIABLE rest INTEGER
CALL c0de/math/DIV
PARAMETER dividend items ;
PARAMETER divisor players ;
RESULT result each
ASSIGN rest items \ players ;
FOR i INTEGER 0 ; players - 1 ;
BEGIN
ASSIGN shares[i] each ;
IF i < rest ;
BEGIN
ASSIGN shares[i] each + 1 ;
END IF
END FOR
END FUNCTION- A shared account is 7 emeralds in debt: -7. Halving the debt with DIV gives -3, but INT of -7 / 2 gives -4. Which is right?
Solution
Both are, for different questions. -7 / 2 is -3.5. DIV rounds towards 0 like FIX, so it says "each of two players owes 3 whole emeralds, and 1 is left". INT rounds down, so it says "the debt of each, rounded to the worse side, is 4". For counting items, DIV and \ belong together: -3 * 2 + -1 is -7 again.
Read more
- The handbook pages of DIV, ROUND, TO_FLOAT, INT and FIX
- Rounding in the guide
- Operators and Expressions:
/and\ - Expressions and statements in the path "c0de for programmers"