-2

I want to be able to construct a numericUpDown field in C# where on each arrow click the value in the field increases by one resolution.

For example;

Let's say my value is 20.0, and it's IEEE754 hexadecimal representation is 0x41a00000.

If I click the arrow up button, I want to see the decimal representation of 0x41a00001, which is 20.0000019073

Likewise, If I click the arrow down, I should see 19.9999980927.

I tried setting the Increment property of the numericUpDown as the 0.0000019073, but I think this value is not constant in IEEE754.

To be more concise, if the value is 700.0, it's IEEE754 representation is 0x442f0000, but 0x442f0001 corresponds to 700.000061035, so in this case it increments by 0.000061035 instead of 0.0000019073.

Thank you.

efedoganay
  • 133
  • 2
  • 11
  • What about incrementing by `flot.Epsilon`? –  Oct 07 '20 at 07:20
  • The issue with this is that since numericUpDown's data type is decimal, event is not fired when it is changed by +-1.40129846432e-45 – efedoganay Oct 07 '20 at 09:04
  • Which event is not being fired? From your post, it seems that an event firing (user pressing a button) should modify the decimal. –  Oct 07 '20 at 09:06
  • Sorry forgot to mention, valuechanged event. I think 1.40129846432e-45 is converted to decimal as 0.0 and event can't handle it. – efedoganay Oct 07 '20 at 09:09
  • What about doing `numericUpDown += decimal.Epsilon`? Also decimal doesn't have a `increment` property, so what type are you actually using? –  Oct 07 '20 at 09:18
  • By Increment I meant NumericUpDown's property. Decimal doesn't have an Epsilon property, – efedoganay Oct 07 '20 at 09:23
  • 1
    Aah sorry I'm not well versed in winforms so I misunderstood. I've done a bit of research and better understand it now. According to other answers, 10^-28 is the smallest value of a decimal, so 10^-45 will be far too small. If you really need to be sure, you could modify the decimal at a bit level, but I think stick to 10^-28 for the moment. Another thing is that I don't believe a decimal is IEEE754, only float and double, so it wouldn't work anyway –  Oct 07 '20 at 09:27
  • Yes, I agree. so any GUI element having a value type of decimal wouldn't work. – efedoganay Oct 07 '20 at 09:32
  • Perhaps [NextAfter(Double, Double)](https://learn.microsoft.com/en-us/dotnet/api/java.lang.math.nextafter?view=xamarin-android-sdk-9)? – chux - Reinstate Monica Oct 08 '20 at 00:30

1 Answers1

-2

Use something like following :

            float start = 20.0F;
            byte[] bytes = BitConverter.GetBytes(start);
            int integer = BitConverter.ToInt32(bytes, 0);
            string hex = integer.ToString("X8");
            integer++;
            string hex2 = integer.ToString("X8");
            byte[] bytes2 = BitConverter.GetBytes(integer);
            float end = BitConverter.ToSingle(bytes2,0);
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • then the corresponding increment would be end-start right? – efedoganay Oct 07 '20 at 08:09
  • Yes. I would change int to Uint32. – jdweng Oct 07 '20 at 09:27
  • You need to use unsigned. The sign bit is bit 31. If you start with a negative number does it work? I think the problem is when you transition between zero and -1. Zero is 0x00000000 while -1 = 0xBF800000. – jdweng Oct 07 '20 at 12:19
  • 3
    This does not work for negative numbers because the floating-point format uses sign-and-magnitude. When the sign is negative, adding one to the representation moves the number to a more negative value, instead of increasing it. There is no transition between 0 and −1; the `float` values near 0 are −2^−149, −0, +0, and +2^−149. – Eric Postpischil Oct 07 '20 at 14:09
  • The code work for negative number, but you have to have limits when you reach the largest positive number, smallest positive number, largest negative number, smallest negative number, and when you transition between positive and negative numbers. – jdweng Oct 07 '20 at 14:15
  • 1
    The code also fails for any number that is just below a power of 2. – Rick James Oct 07 '20 at 21:18