New 'number' type and -number:type option
Posted: 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:
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:
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:
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
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