Dim result As Long
result=24*60*60*180*1000
Log(result)
Log(Abs(result))
Dim direct As Long=1627869184
Log(direct)
-1627869184
1.627869184E9
1627869184
99.9% of the numbers that you use in your code are in the range of int. Treating them as long will have some performance and memory impact.but are the numbers "counted" as INTs because: for less memory ?
YesWill it always work as long as only ONE of them is a long and the rest are all ints?
Java promotes values to that of the most precise type in the expression before doing a calculation
You don't want to treat whole numbers as double by default. It will have bad side effects.
No. If you want to avoid overflow issues, the first number must be a Long. This can be either by ensuring that the first number is a long variable or that the first number is a string literal that must be converted to a long (it is large enough not to fit into an Int).Will it always work as long as only ONE of them is a long and the rest are all ints?
If Java does this, B4X does not mimic that behavior. In order to avoid overflow, the first number used in the calculation should be of the desired output type (in this case Long). See the second link I posted above for proof by code.Making one of the values a long doesn't apparently change the compiled code but does force Java to promote the other values to longs before calculating the result.
1. Changing the behavior of numeric literals, at this point, is not something that can happen as it will break backward compatibility ( [B4X] Backward Compatibility )Which ones? As doubles can represent quite a large integer range precisely (much larger than 32bit Integers) I’m curious which side effects you are referring to?
Interesting. Thank you. You learn something new everyday (or should if you don't)!, the first number used in the calculation should be of the desired output type (in this case Long).
A 2010 Mac Pro is likely to be faster than many mobile devices.ven a 10 year old computer is so fast that you won't notice whether you use 64bit integers or Doubles instead of 32bit integers (and as a Molecular Biologist I used to routinely analyse multi-gigabyte sized files of data on my 2010 Mac Pro).
Public ToDouble(d As Double)
Return d
End Sub
So turning integers into doubles will also produce the correct result when it falls within the 'long' range, right?You can add .0 to all numbers if you want numeric literals to be treated as doubles.
'Method 1
dim n1 as long = 24
Log(n1*60*60*180*1000)
'Method 2
Log(24.0 * 60 * 60 * 180 * 1000)
Yeah, I had forgotten about an annoying experience I'd had with doubles, namely:Method 1 is better. Doubles cannot represent accurately all values.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?