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 CLOSED
 
Thread Tools Search this Thread Display Modes
Old 06 June 2006   #1
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.

So.
var r = 116/256;
prints 0

and
var r = 116/256.0;
correctly prints 0.453125

Cheers
Lennart
__________________
MSA | OSX | xfiles ...Cinema4D Tools
 
Old 06 June 2006   #2
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"
 
Old 06 June 2006   #3
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
__________________
MSA | OSX | xfiles ...Cinema4D Tools
 
Old 06 June 2006   #4
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.
 
Old 06 June 2006   #5
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.
 
Old 06 June 2006   #6
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
CGSociety
Society of Digital Artists
www.cgsociety.org

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

All times are GMT. The time now is 11:39 PM.


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