COFFEE/SCRIPT | Can't divide integers?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  06 June 2006
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;
prints 0

var r = 116/256.0;
correctly prints 0.453125

MSA | OSX | xfiles ...Cinema4D Tools
  06 June 2006
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
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.

MSA | OSX | xfiles ...Cinema4D Tools
  06 June 2006
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
Originally Posted by ascent: That's not all that uncommon in "untyped" programming language
Actually, 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.
  06 June 2006
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 12:54 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.