0

Given a base 10 floating-point number, for example, .583*10^3, can the number be converted into the equivalent base 2 form x*2^y by separately converting the fraction (.583) and the exponent (3) into base 2? If so how could I demonstrate that? Thanks so much for your help!

A_Pointar
  • 121
  • 12
  • 1
    IEEE754 floating point numbers *are* binary (i.e. base 2) floating point numbers. But conversion is not so easy, since you must convert from one base to the other, so you can't simply convert fraction and exponent separately. If you change the base of the exponent, the value of the fraction must change accordingly. Also note that you won't always get a one to one conversion. – Rudy Velthuis Sep 16 '14 at 07:11
  • Floating-point libraries in most languages are doing that already. It's not a trivial task if you need the correct rounded result – phuclv Apr 08 '15 at 12:46
  • possible duplicate of [How to manually parse a floating point number from a string](http://stackoverflow.com/questions/85223/how-to-manually-parse-a-floating-point-number-from-a-string) – phuclv Apr 08 '15 at 12:49
  • http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i – phuclv Apr 08 '15 at 12:50

1 Answers1

1

Not all base 10 floating point numbers can be converted exactly to a base 2 floating point number. For example 0.2 can be exactly represented in base 10 floating point but cannot be represented in base 2 floating point with a finite number of digits 0.001100110011001100110011001100110011001100110011001101... which rounds to about 0.200000000000000011102230246251565404236316680908203125.

A related article is Why can't decimal numbers be represented exactly in binary.

If you're just interested in an approximation, there are various libraries that can do this for you. For example, atof("3.14") in C will convert the string of digits into your machine's native floating point representation from which you may be able to extract the binary digits.

Community
  • 1
  • 1
ceilingcat
  • 563
  • 3
  • 10