B4R Question Incrase/Decrase Float by 1

mw71

Active Member
Licensed User
Longtime User
Hi,

is there a way to incrase or decrase a float by 1?
Inc/Dec by 10, 100, all o.k., but with 1 no change.
 

mw71

Active Member
Licensed User
Longtime User
no, 27000000
with +/- 10 or 100 all o.k., but not with 1

Code like this:
B4X:
Dim X as Float

'and in the Sub, dependent on Encoder
X=X+1 'or X=X-1
 
Upvote 0

maXim

Active Member
Licensed User
Longtime User
Hi mw71,

try with:

B4X:
x = x + 1.0  'or x = x - 1.0
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
I'll interpret that as: yes, it is larger than 16 million
:oops:, you rigth.....

Change the Float to a Double. Or a Long, if it is only ever whole numbers ie no fractional part.

i have change to ULong (after check the dependencies), now it works

Hi mw71,

try with:

B4X:
x = x + 1.0  'or x = x - 1.0
unfortunately that does not work (test in B4R direct an inline c...)
 
Upvote 0

emexes

Expert
Licensed User
now it works
Super!

The reason was probably: a Float (single precision) is 1 32-bit type. 8 bits are used for the exponent, 1 for the sign, and 23 bits for the mantissa (the actual number part).

There is a bonus mantissa bit because we know it does not begin with a "0" (similar to the way that, when you write a number, you don't write leading zeroes).

24 bits can represent numbers up to 2^24 = 16-and-a-bit million. Once you go higher, to numbers that need 25 bits, the lower bit is "lost" and consequently the Float number can now only represent numbers where that lost bit is zero, ie even numbers / every second number.

A 32-bit ULong can hold numbers up to (one less than) 2^32, so that'll get you to just over 4 billion (4 GHz?).
 
Last edited:
Upvote 0
Top