Conversion to and from based real numbers

  • Thread starter Tapio Kelloniemi
  • Start date
T

Tapio Kelloniemi

Hi

I didn't find this one from the FAQ:
I'm writing an input parser for numbers. Everything else is piece of
Cake, except how to parse real numbers that are in some other base
than 10, eg. 3A.B4 in base 12. This can be done for integers using
the strtoxx ISO C functions, but there is no base argument for the
float/double versions. How could I then print out reals in non-base 10
format?

THanks for answers.
 
C

CBFalconer

Tapio said:
I didn't find this one from the FAQ:
I'm writing an input parser for numbers. Everything else is piece
of Cake, except how to parse real numbers that are in some other
base than 10, eg. 3A.B4 in base 12. This can be done for
integers using the strtoxx ISO C functions, but there is no base
argument for the float/double versions. How could I then print
out reals in non-base 10 format?

For input, assuming you already know what the base is, parse the
integer portion with strtoul. That leaves a pointer to the
termination character, which you can check to be a '.'. Advance
the pointer. Check it points to a digit in the appropriate base.
Parse the remainder. The returned value represents itself divided
by base raised to the number of chars consumed. Combine
components.

Output of the integer portion is easy. The fractional portion can
be handled by successive multiplications by the base and
converting the integer portion of that into a digit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top