-1

I am writing program which takes floating number from input and outputs hex representation of this number.

What I did to solve it was:

  1. Divide number to whole and decimal part.
  2. Convert whole and decimal parts to binary representation. a) Whole number divided by 2, if number mod2 ==1, add 1, else add 0. Turn whole number around. b) Decimal part is being multiplied by 2, if it's higher than 1, substract 1 and do it again.
  3. If number is less than 0, sign is 1, else sign is 0.
  4. Get exponent (127+/-x), convert it to binary (whole).
  5. Get mantissa.
  6. Convert sign+exponent+mantissa to hex.

My program is passing through every test there was on SPOJ forum. I had to look for manual test cases where it fails myself.

So in case of number -123123.2323 I received number:

(hex) c7 f0 79 9d

Binary whole=11110000011110011 Binary decimal=0011101101111000000000 ....

Mantissa=11100000111100110011101

Meanwhile https://www.h-schmidt.net/FloatConverter/IEEE754.html gives me:

Mantissa=11100000111100110011110

How does it work and why this way in that case? Using https://www.rapidtables.com/convert/number/decimal-to-binary.html?x=0.2323 when I convert 0.2323 to binary it gives me 0.0011101101111. I used this part to finish the mantissa after adding whole binary number (-1), up to 23 bits. What am I doing wrong?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Advent
  • 140
  • 15
  • 2
    Post your "My program is passing through every test" code. – chux - Reinstate Monica Jun 19 '18 at 13:13
  • I just gave you output which I get from my program and the way I calculate mantisa. All I want to know is why -123123.2323 mantisa is 11100000111100110011110 instead of 11100000111100110011101. – Advent Jun 19 '18 at 13:15
  • 2
    Your description of code is not the best evidence and what is written lacks important details. Your code is best and provides clarity. Note that a 32-bit float can represent _exactly_ about 2^32 different values. -123123.2323 is not one of them. The closest is -123123.234375 and the next closest is -123123.2265625 A clear answer to all concerning "What am I doing wrong?" will remain hidden as long as your code is hidden. – chux - Reinstate Monica Jun 19 '18 at 13:55
  • Actually if I take -123123.234375, it gives correct value. It means that I need to rewrite my program. It has to do something with float. I wrote simple program taking input and outputing value with precision set and when I use double type and give -123123.2323, it gives this value back. When I use float, I get 123123.2344 in return. I think I know what to do. Instead of getting input as string, I will take it as float, then cast it to string and progress with said value. This will work. Anyway, could you give me some information why I receive this value and how to calculate it myself? – Advent Jun 19 '18 at 16:11
  • "Anyway, could you give me some information why I receive this value and how to calculate it myself? " Yes, certainly. Seeing your code here would simplify matters. – chux - Reinstate Monica Jun 19 '18 at 16:14
  • https://pastebin.com/d0MxrMg3 https://pastebin.com/0WYiMM2u I would like to just understand how calculating floating point number works like. I know how to make it work now since I understand the reason why it gave me wrong results. – Advent Jun 19 '18 at 16:18

2 Answers2

1

0.2323 and -123123.2323 are not exactly equal to any IEEE-754 float. Anything you use to convert it to a binary floating-point representation will need to choose an amount of precision to use, and which direction to round it in. It looks like the various things you've used for that made those choices differently.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • What do you mean? I used https://www.wikihow.com/Convert-a-Number-from-Decimal-to-IEEE-754-Floating-Point-Representation tutorial to understand how to get mantisa from decimal number. I am not saying that 0.2323 and -123123.2323 are equal. I am saying that when I am trying to calculate mantisa, I used 123123 binary representation, then add binary represntation of 0.2323. Do you mean that my number should be rounded up and then calculated? – Advent Jun 19 '18 at 13:25
  • 1
    I'm saying, there is no exact, finite-length binary representation of 0.2323. Your web page is misleading you. – Sneftel Jun 19 '18 at 13:31
  • Yeah, I am counting up to 128 binary bits but mantis only consist of 23 bits. – Advent Jun 19 '18 at 13:33
0

Alright so it's not like my program or solution was wrong, just my thought process and I lacked crucial information about floating point numbers.

As Mr. "chux" said, the number I tried to compute (-123123.2323) should be read by computer as other number (-123123.2344). Since I used string for input and simply divided number into whole and decimal parts, I couldn't see floating point numbers limitations.

Solution is to read number as floating point number, let computer change it's value and then read it as string and work with that.

Advent
  • 140
  • 15