Mixing doubles and numbers

Without conversion functions (see math-functions), double and number expressions do not mix, as they are separate types. One exception to this is using numbers (i.e. whole numbers) in double expressions, since in that case they are automatically promoted; the reason for this is that this conversion is lossless.

The opposite is not true; doubles would generally lose precision when promoted to numbers, and for this you can use floor(), ceil(), round() etc.
Using numbers in double expressions is fine
For example, the following is valid, because a number (3) is promoted to a double:
set-double d = 4.5 + 3

Using doubles in number expressions is not, however...
In general, a double cannot be automatically promoted to a number without loss. In some languages, there is an automatic promotion of this kind, however it is a bad idea and a source of potential bugs since the method of demotion can vary and often the developer isn't aware of it:
set-number n = 3 + 4.5  // this won't work

Instead you can use functions like num(), round(), ceil(), floor() etc. (see math-functions):
set-number n = 3 + round(4.5)

Comparisions
When it comes to comparisons, the type determination is performed from left to right. So the following works:
if-true 3.5 > 3
    @Yes
end-if

because a double (3.5) comes first and then a number (3) is promoted to a double. However, this will not compile:
if-true 3 < 3.5
    @Yes
end-if

because a number (3) comes first and then a double (3.5) cannot be automatically promoted to a number. When writing comparisons between the two, either use the appropriate conversion functions, or write double expressions first. For instance you can use dbl() function (see math-functions):
if-true dbl(3) < 3.5
    @Yes
end-if

See also
Doubles
abs-double  
double-expressions  
double-string  
mixing-doubles-and-numbers  
set-double  
string-double  
Numbers
abs-number  
mixing-doubles-and-numbers  
number-expressions  
number-string  
set-number  
string-number  
See all
documentation


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.