B4R Question Arithmetic expressions

positrom2

Active Member
Licensed User
Longtime User
How a composite arithmetic expression like A=(C-D)/(e+f) is evaluated in case the numbers are of mixed types?
If C is float (Double) and D,A are int, which type will be the intermediate C-D?
If C-D=.1, and e+f=.1, A=0 or A=1?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If C is double then D will also be converted to a double.

If C-D=.1, and e+f=.1, A=0 or A=1?
It should be 1. However it is safer to use Round:
B4X:
A = Round(...)
Otherwise a small rounding error can cause it to be 0 instead of 1.
Remember that decimal values cannot be accurately represented in a floating point value.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Otherwise a small rounding error can cause it to be 0 instead of 1.
Thanks,
so that might have been the problem in my "attempt" to fit the BMP280 readings like 123456.89 into the OLED int 0-63 range...
I am coming from another Basic dialect (Bascom) that allows just one operation per line.
In that case, it is more obvious what happens though quite tedious to disentagle a more composite equation.
 
Upvote 0
Top