Forums

New 'number' type and -number:type option

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

New 'number' type and -number:type option

Postby Charles » Sun Mar 16, 2008 10:04 pm

Cobra defaults to using 'decimal' instead of 'float64' or 'float32' because decimal can accurately represent the numbers that people often input such as 0.1. As an added bonus, decimal has more significant digits and a wider range of values. By "Cobra defaults to 1" I mean that:
class Sample
def main is shared
rate = 0.1
# rate's type is decimal, not float
# or consider:
.util(4.5) # woops. compile-time error. forgot to say 4.5f
def util(x as float)
pass

The benefits of decimal are covered in more detail under "Accurate Math" in the Comparison to Python.

But decimal is not perfect. There is a performance penalty (which I've never gotten around to measuring) and there is the fact that many libraries oriented towards graphics, mathematics, games, neural nets, etc. will be based on float64. This is made more painful by the fact that .NET generic classes cannot be parameterized by numeric type in any meaningful way.

I certainly want developers in those areas to feel welcome and productive with Cobra. Its compiled performance, slick syntax, unit tests and (optional) static typing make it an otherwise great choice for those areas.

So in development, I have added a new -number:TYPE command line argument. The choices for TYPE include the fractional numeric types in the language: float32, float64, float and decimal. ('float' is an alias for 'float64') The default is 'decimal' but you can override this now like so:
Code: Select all
cobra -number:float myprog.cobra

Now the literal "4.5" will be typed as 'float', not 'decimal'. No "f" suffix required.

Furthermore, you can use the keyword 'number' as a type now:
def sum(a as number, b as number) as number
return a + b

If you use 'number' throughout your code and you are not passing your values to a library, then you can change the definition of number to see how that affects the accuracy and speed of your program. But the primary motivation for this feature is to make life easier for programmers that normally work with float.

If you have any questions, please ask. If you test this out of the repository and have any problems, let me know here in the forums.

-Chuck
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: New 'number' type and -number:type option

Postby Charles » Sun Mar 16, 2008 10:08 pm

By the way, you can always dictate the type of a fractional literal by using a suffix. This works independent of the -number: option.
d  = 1.0d    # decimal
f3 = 1.0f32 # float32
fa = 1.0f # float / float64
fb = 1.0f64 # float / float64
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: New 'number' type and -number:type option

Postby hopscc » Mon Jun 02, 2008 3:35 am

If you have multiple source files that require different types for 'number' (e.g 3 use default decimal and one needs float64 say) having to specify them on the commandline
a) forces you to use at least two command line invocations (which)
b) demands you use a build system or script to keep the typing and file info together.
It would be better if this default number' typing was also settable in the cobra source file ..... like in a compiler directive ...

Heres a patch that adds a compiler directive doing just that - sets the default type of 'number' to the specified type from the directive on.
Code: Select all
%% number float32
    def y is shared
        m as number = 48
        print '[m.getType] m=[m]'
        assert m.getType is Single

%% number decimal
    def x is shared
        m as number = 46
        print '[m.getType] m=[m]'
        assert m.getType is Decimal
Attachments
numberDirective.patch
(1.95 KiB) Downloaded 603 times
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: New 'number' type and -number:type option

Postby hopscc » Mon Jun 02, 2008 3:41 am

And unit test file
Attachments
numberDirective.cobra
(923 Bytes) Downloaded 611 times
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 81 guests

cron