Forums

Math

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Math

Postby dennis » Sun Feb 10, 2008 9:56 pm

When I try something like:

print Math.sin(Math.pi / scale)


(scale is a Double)

I get errors like:

Cannot apply SLASH to Double.


Cannot find a definition for "pi" in "Math".


Do I need to do some sort of type casting or overloading that I'm overlooking?
dennis
 
Posts: 21

Re: Math

Postby Charles » Sun Feb 10, 2008 10:57 pm

Since Cobra member access starts with a lower case letter, it's "pI". Certainly "pi" should be accepted and I'll enhance this. Not sure about the other error. I'd have to see the rest of your code. This works for me:
class X
def main is shared
scale = 1.0f
print Math.sin(Math.pI / scale)

To reduce the ugliness, you could say:
pi = Math.pI

And use "pi" from then on.
Charles
 
Posts: 2510
Location: Los Angeles, CA

Re: Math

Postby dennis » Mon Feb 11, 2008 9:16 am

I didn't think to try that capitalization.

Here is a sample of code that produces the SLASH error:

"""
MathTest.cobra
"""

class MathTest

shared

var _c as Double

def main

_c = 35.12 to Double
d = 1.456 to Double
print _c / d


In my code, I can probably do the arithmetic in another type, but I need to cast it to Double to pass it to the trig methods. And it would require doing about twice as many type casts that way.
dennis
 
Posts: 21

Re: Math

Postby dennis » Mon Feb 11, 2008 10:34 am

Using float instead of Double takes care of the SLASH error.
dennis
 
Posts: 21

Re: Math

Postby Charles » Mon Feb 11, 2008 11:12 am

Right, Cobra's primitive types for numbers are now:
  • int, uint (default 32 bits)
  • int8, int16, int32, int64, uint8, uint16, uint32, uint64
  • decimal
  • float
The float is 64-bits. There will be a float32 and float64 some day.

Suffixes:
  • f for float. ex: 1.5f
  • d for decimal, but this is optional as it's the default.
  • for integers, cast to the type: 1 to int64
We'll likely need suffixes for the integers as well so you can have a 64-bit literal without "breaking it down" into an arithmetic expression. My current thought for int suffixes would be, for example, "100i64" and "100u64" for signed and unsigned.

Also, you can put underscores in numbers like so: "i = 10_000_000". That's 10 million. Works for floats and decimals too.
Charles
 
Posts: 2510
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 11 guests