hedyorg / hedy

Hedy is a gradual programming language to teach children programming. Gradual languages use different language levels, where each level adds new concepts and syntactic complexity. At the end of the Hedy level sequence, kids master a subset of syntactically valid Python.
https://www.hedy.org
European Union Public License 1.2
1.33k stars 290 forks source link

🪲 Precision of floating points calculations #5819

Open jpelay opened 1 month ago

jpelay commented 1 month ago

A teacher reported this to us in an email:

Hi,

Level 12 Calculator - Multiplying Decimals

When the code is run and the player answers 1.95 the output is 'Wrong!'

number1 = 1.3
number2 = 1.5
player_answer = ask 'What is ' number1 ' multiplied by ' number2
correct_answer = number1 * number2
print 'your answer was ' player_answer
print number1 ' times ' number2 ' is ' correct_answer
if player_answer = correct_answer
    print 'Correct!'
else
    print 'Wrong!'

I checked and the reason is IEE754, because this is the result that Pythons gives:

>>> 1.3*1.5
1.9500000000000002
>>> 1.95 == (1.3*1.5)
False
>>>

So, two lines of argumentation can be given with respect to how we can deal with things like these:

In pro of keeping this behavior: since Hedy is teaching programming to kids, and the end goal is to end up programming in Python, this behavior should be kept. Ultimately, this is how floating point numbers are going to work in pretty much every programming language. Changing the above comparison to work, would cause confusion if they eventually move to Python and then figuring out that floating point numbers are funky.

Against: We should change it because we are trying to simplify programming, and teaching it step-by-step, and part of that requires simplifying how floating point numbers work, so it's up to students to later on adapt to the realities of IEE754. There must be a library that has floating point numbers that work like the ones we are used to, specially for accounting, where such things are essential.

arjank commented 1 month ago

A quick Google search led me to an answer on StackOverflow which pointed to the Decimal type.

jpelay commented 1 month ago

A quick Google search led me to an answer on StackOverflow which pointed to the Decimal type.

It's not implemented in Skulpt 😢 so we can't use that :/

AnneliesVlaar commented 1 month ago
Scherm­afbeelding 2024-10-19 om 08 34 01

I had a similar (?) problem in level 6. I would expect 360/19 to be 19 not 18. I agree with 'against keeping this behavior'.