Skip to content
This repository has been archived by the owner on Dec 5, 2018. It is now read-only.

add math floor while converting int to var #50

Closed
wants to merge 1 commit into from
Closed

add math floor while converting int to var #50

wants to merge 1 commit into from

Conversation

nok
Copy link

@nok nok commented Feb 5, 2013

@Pomax
Copy link
Member

Pomax commented Feb 5, 2013

This code is actually superfluous; if the sketch works in Processing, the value will already be rounded (because you can't declare int something = decimal fraction, it'll throw an error). So it would only make sense to persistently force ints, which is impossible in JavaScript: even if we floor it at parse time, any subsequent operation may introduce a decimal fraction. So there's actually no benefit to ensuring an int at js-conversion time.

Which issue did you run into that was fixed with this?

@nok
Copy link
Author

nok commented Feb 5, 2013

Please have a look to that issue.

int result = 3/2;
println(result); // 1.5

If the user writes Java code, he expects Java results? It's a dilemma.

@Pomax
Copy link
Member

Pomax commented Feb 5, 2013

Yes, we explain this in the "quickstart guides" for Processing and Javascript devs. There are no ints in JavaScript, it's all float. Just fixing int at parse time won't solve this.

int v1 = 2, v2 = 3, v3;
...
v3 = v1/v2;

this is the same issue, but can't be properly solved without actually adding in our own type tracking, which will slow things down an incredible amount. This fix would only fix a few of the many cases in which int division won't work, so instead of keeping it a well known, and documented effect, this makes int division "sometimes work, sometimes not", which is less useful than "never works" since you can rely on that.

We explain this in http://processingjs.org/articles/jsQuickStart.html#explicitcasting and http://processingjs.org/articles/p5QuickStart.html#explicitcasting so I'm going to close this pull request - it's an annoying thing when you first run into it, but the workaround are very simple and involve applying normal programming things (no hacks required, just tactical use of floor() or round() depending on what you really want). Sadly, anything that isn't "add strong typing ourselves" will turn the behaviour into something unpredictable, which is a bad idea. People need to be able to rely on the fact that somethings' always going to behave the same way, or debugging will become hell for them.

Thanks for wanting to help out, though!

@Pomax Pomax closed this Feb 5, 2013
@nok
Copy link
Author

nok commented Feb 5, 2013

Learning never stops. 👍 Thank you for the extensive explanation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants