Lang::Number.toNumber() behaviour

Former Member
Former Member
The API docs don't describe how this behaves. I did a quick test, and it behaves like Math.floor() if the number is positive, but like Math.ceil() if the number is negative. e.g.,

3.2 => 3
3.9 => 3

-3.2 => -3
-3.9 => -3

Worth updating the docs I think.
  • This is the result of a simple float-to-integral conversion. It rounds toward zero and it occurs any time you convert a floating-point value (Double or Float) to an integral value (Long and Number). This isn't anything new, and is consistent with all of the languages that MonkeyC is based on (Java, Python, Ruby, C).

    There are _many_ things that need to be documented about the MonkeyC language and interpreter. While this is indeed something that should be documented (it is documented for each of the other languages), it seems that it would be documented as part of a larger document that describes the language specification instead of just a note thrown into the existing programmers guide.

    Travis
  • Former Member
    Former Member over 10 years ago
    Thanks. I'd love to read a language specification for Monkey C. But until there is one, can you suggest which other language is closest?

    It is very frustrating trying to deal with compile-time errors at the moment. e.g.,
    some lines need a ; on the end, but others give errors with a ; on the end. Can't have variables called e1 or e2 but I have no idea why, etc.
  • I'm most familiar with C/C++, and have a little bit of python experience, so I'm using those as the basis for my understanding of MonkeyC. That said, there are lots of things that are different, so I find myself experimenting for hours trying to figure out what is going on.

    I'm able to reproduce the problem with variables names that match the expression e[0-9]+. My guess is that this is because the variable name is somehow being interpreted as a the exponent part of a floating-point constant. i.e., you can write the floating point value 1000 as 1000.0, or as 1e3, and I'm guessing the bit of code that does this parsing is screwing things up.

    As for the semicolon issue, I think MonkeyC is very similar to C/C++ in this respect. The only difference that I'm seeing is that MonkeyC doesn't allow them at the end of a user-defined-type declaration, where as they are required to be there for C/C++.
  • Former Member
    Former Member over 10 years ago
    I'm most familiar with C/C++, and have a little bit of python experience, so I'm using those as the basis for my understanding of MonkeyC. That said, there are lots of things that are different, so I find myself experimenting for hours trying to figure out what is going on.


    Thanks, glad it isn't just me ;) I develop mostly in PHP/Javascript, so again lots of differences.
  • Can't have variables called e1 or e2 but I have no idea why, etc.

    I've filed a bug report so we'll take a look into this one. Thanks for the report.

    I've filed another report to update the toNumber() documentation.