4 kyu

Float to Binary Conversion

Description:

I need you to implement a function called float2bin that converts a float number to the corresponding 32-bit binary value. This involves analysing each part of the input number and appointing each bit of the output accordingly. See visual explanation from wiki below:

IEEE754 visual

Rules:

  • The conversion should match the IEEE754 Single precision 32-bit technical standard.
  • The implementation should be able to handle both Strings ("123", "1.23") and Numbers (123, 1.23) as input.
  • It should convert both positive and negative numbers.
  • The output should always be a 32 characters long string, so padding of 0's is necessary if input is not negative.
  • The point of this kata is to teach how floats are build on a binary level, so typed arrays are disabled.

Expectations and limitations:

  • The input is valid numbers or strings of numbers.
  • No weird edge cases are tested.
  • Since there always is some rounding inaccuracy when working with floats, the last 5 characters in the output will not be checked. This is done using something similar to the approx function in the test fixture.

Here are some links to help you:

Bits
Algorithms

More By Author:

Check out these other kata created by MRodalgaard

Stats:

CreatedSep 8, 2014
PublishedSep 8, 2014
Warriors Trained1490
Total Skips590
Total Code Submissions2304
Total Times Completed257
JavaScript Completions257
Total Stars110
% of votes with a positive feedback rating98% of 70
Total "Very Satisfied" Votes68
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes1
Ad
Contributors
  • MRodalgaard Avatar
  • jhoffner Avatar
  • trashy_incel Avatar
Ad