PDA

View Full Version : COFFEE/SCRIPT | Can't divide integers?


tcastudios
06-09-2006, 04:25 PM
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.

So.
var r = 116/256;
prints 0

and
var r = 116/256.0;
correctly prints 0.453125

Cheers
Lennart

unseenthings
06-09-2006, 05:08 PM
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" :)

tcastudios
06-09-2006, 05:17 PM
Thanks. Good to know:) Concidering I have written a couple of thousends of lines by now, it's strange that I haven't stumbled on it before.


Cheers
Lennart

Darter
06-11-2006, 09:47 PM
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.

arctan
06-12-2006, 06:30 PM
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.

CGTalk Moderation
06-12-2006, 06:30 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.