O
O
Olzhas2015-09-30 11:44:40
Programming
Olzhas, 2015-09-30 11:44:40

How to convert fractional numbers from binary to 10 in c++?

You need to write a code for converting fractional numbers from 2 to 10

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mrrl, 2015-10-01
@Mrl

You can do so. Create two variables: double x=0, y=0.
Read the line from left to right. If the digit came b=='0' or '1', then execute x=2*x+b-'0'; y*=2;
If a comma came - assign y=1. Finally, if y is non-zero, then divide x by y.
For example,
for line 101 : x=5, y=0. Answer 5.
101.101 : x=45, y=8. Answer 45/8=5.625
101, : x=5, y=1. Answer 5.
,101 : x=5, y=8. The answer is 5/8=0.625.

A
Anton Fedoryan, 2015-09-30
@AnnTHony

Floating point/point numbers then.
Here is the IEEE 754 standard for you if you are completely banned in Google.
Convert to binary, and then rotate as you please, even in hex, even in octal with ternary.

S
Saboteur, 2015-09-30
@saboteur_kiev

Take separately the bits in which the fractional part is stored, separately the integer part. Convert to decimal, add.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question