|06 June 2006||#1|
Join Date: Jan 2003
COFFEE/SCRIPT | Can't divide integers?
Is this correct?
I'm using RGB values to set colors. To use the values as vectors I divide each value by 256.
However that gives me 0 (Zero).
If I add a single decimal, it works fine.
var r = 116/256;
var r = 116/256.0;
correctly prints 0.453125
MSA | OSX | xfiles ...Cinema4D Tools
|06 June 2006||#2|
Where's my hasenfeffer??portfolio
Join Date: Feb 2003
That's not all that uncommon in "untyped" programming language (where you don't specifically identify an item as an integer or float on the front end when you define it) -- without the .0 it doesn't know that you want a float result, so it just gives you the integer result (rounded, I think).
So yeah, you're doing it the right way. That's a "feature"
|06 June 2006||#4|
Join Date: Nov 2003
As ascent suggests, you can also use float() on one of the variables, e.g. var r = float(116) / 256;
This can be useful if the variable is generated elsewhere in the code.
Last edited by Darter : 06 June 2006 at 09:53 PM.
|06 June 2006||#5|
Join Date: Nov 2005
Originally Posted by ascent: That's not all that uncommon in "untyped" programming languageActually, it goes back to COFFEE's roots in C++, which is very much a typed language: without a decimal point, a numeric literal is considered an integer. An integer divided by another uses an integer divide, with integer result. In mixed expressions, the integers are all promoted to floats, and you get a floating point divide with a float result.
It really helps to understand COFFEE if you also know C++, because there are a lot of carryovers like this.
|Thread Closed share thread|