B4R Question Incrase/Decrase Float by 1

mw71

Well-Known 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.
 
no, 27000000
with +/- 10 or 100 all o.k., but not with 1

Code like this:
Code: Copy
Dim X as Float

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

try with:

Code: Copy
x = x + 1.0  'or x = x - 1.0
 
Upvote 0
Upvote 0
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
Back
Top