Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Zero point zero isn't zero #582

Closed
TheLogFather opened this issue Jan 7, 2015 · 6 comments
Closed

Zero point zero isn't zero #582

TheLogFather opened this issue Jan 7, 2015 · 6 comments
Assignees
Labels
Milestone

Comments

@TheLogFather
Copy link

Try subtracting zero from 0.0 (e.g. put into a say block).
Or try ceiling of (0.0). :/

See this topic: http://scratch.mit.edu/discuss/topic/79402/

Also, see this project: http://scratch.mit.edu/projects/36859670/

@2jour 2jour self-assigned this Jan 7, 2015
@2jour 2jour added this to the Review milestone Jan 7, 2015
@cpseager
Copy link
Contributor

cpseager commented Jan 7, 2015

This ia common issue in any computer language - it's a floating point versus integer thing.

The 0 is a Number type, but is internally stored as an Integer because there is no decimal point.

The 0.0 is also Number type, but due to the decimal point is now internally stored as a floating point instead, and as with all floating point numbers gets rounded a little bit, in this case to:

2.3283064E-010
(0.00000000023283064)

The solution is for Scratch to round the floating point numbers to less decimal places before using them:

 var setPrecision:Function = function(number:Number, precision:int) {
   precision = Math.pow(10, precision);
  return Math.round(number * precision)/precision;
 }

@TheLogFather
Copy link
Author

Oh, I see where it's happening:

http://github.com/LLK/scratch-flash/blob/master/src/blocks/BlockArg.as#L231

Hmmm...

Maybe another way to 'signal' that it's a float rather than int might be to extend "isNumber" so it's no longer just true/false? E.g. zero, one, two mean, respectively, non-numerical, integer, float? Or add a new boolean "isFloat"?

@cpseager
Copy link
Contributor

cpseager commented Jan 8, 2015

Yes, seems very strange that the Scratch code is deliberately introducing a mathematical error to workaround that Flash is (correctly) deciding that 4.0 is the same as 4 and so defining it as an integer!

@kaschm
Copy link

kaschm commented Jan 9, 2015

@cpseager, @TheLogFather - thanks for submitting this issue. We're looking into why this was set up in this way; the code predates most of the team :) Stay tuned for an update.

@kaschm kaschm modified the milestones: Review, v432 Jan 9, 2015
@kaschm kaschm assigned cwillisf and unassigned 2jour Jan 9, 2015
@cpseager
Copy link
Contributor

cpseager commented Jan 9, 2015

See
https://github.com/LLK/scratch-flash/blob/master/src/primitives/Primitives.as#L99

I guess it was assumed that if you wrote something like 4.0 you wanted to work to 1 decimal place rather than as an integer - but this particular routine checks both parameters to see if they are both integers and then gives a different result then if they were floats.

However deliberately introducing a mathematical error for everything to workaround this particular 'random' command result is probably not the correct way to handle this!

@TheLogFather
Copy link
Author

And I guess https://github.com/LLK/scratch-flash/blob/master/src/blocks/BlockArg.as#L164 is why large integer args typed into some blocks will quite often get ".0" appended to them for no apparent reason. I've wondered about that for quite a while...

It'd be nice to iron out these idiosyncrasies.

Does anyone know of another block (i.e. apart from "pick random") that behaves in a different way depending upon its args being int vs decimal?

@2jour 2jour modified the milestones: v433, v432 Jan 29, 2015
@2jour 2jour added ready and removed ready labels Jan 30, 2015
@2jour 2jour modified the milestones: v434, v433 Feb 19, 2015
@kaschm kaschm removed this from the v434 milestone Mar 10, 2015
@sclements sclements assigned sclements and unassigned cwillisf Apr 5, 2015
@sclements sclements added this to the v435 milestone Apr 5, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants