I am trying to do a program to convert decimals to floating point binary number but I can't seem to store the binary scientific notation inside a variable. So far I only can print the binary in scientific notation using %e, is there anyway to store this print value into a variable? Because want to use this variable to do my mantissa.
This is the code to print:
printf("\nExponent: %e", fraBinary);
fraBinary refers to my binary in floating format.
for example:
fraBinary = 1010.011001
print out in scientific notation = 1.010011001
i want to store this 1.010011001 into a variable so that i can use this variable in other parts of my program.
This is C program.
CodePudding user response:
Although C's floating-point types float
and double
typically use binary representations internally (almost invariably using formats conforming to IEEE-754), there are no standard routines for printing floating-point numbers in binary, or to convert binary strings to floating-point.
Basically there is almost no need for human-readable binary floating-point strings; floating-point numbers are almost always converted to and from decimal strings. (This causes lots of problems, though, since binary and decimal fractions are incommensurate, and always end up being imprecise approximations for each other — but that's a different story.)
The closest you can come to a binary floating-point string involves the %a
format, although it's hexadecimal, not binary. But %a
with printf
will convert a floating-point number to a hexadecimal string, and %a
with scanf
will convert a hexadecimal string back to a floating-point number.
It is probably easier to explain this by example. Suppose I have the decimal number 12345.53125, and suppose I store it in a float
variable:
float f = 12345.53125;
I can print it back out using %e
or %f
:
printf("%e\n", f);
printf("%f\n", f);
This prints
1.234553e 04
12345.531250
If I print it out using %a
I get the rather odd-looking
0x1.81cc4p 13
In actual binary this number is
0b11000000111001.10001
or if you want an exponential notation it might be
0b1.100000011100110001e13
But, as I said, there's no built-in C function that will generate binary representations like that for you automatically.
If you did something like
double fraBinary = 1010.011001;
printf("%.9e\n", fraBinary);
and saw
1.010011001e 03
then you were not working with binary numbers at all: those numbers 1010.011001
and 1.010011001e 03
you saw and typed were pure decimal.
When you're working with ordinary floating-point numbers (using scientific notation or not), you don't usually need to worry too much about the binary representation (other than to know that it's there), and you don't usually have to work with significands ("mantissas") and exponents separately. If you don't already, you should know that the notation exemplified by
1.234553e 04
is almost universal. You can enter constants in yur program that way:
double d = 1.234553e 04;
You can scan numbers from the user that way. You can take strings like "1.234553e 04"
and convert them to float
or double
by calling the library functions atof()
or strtod()
. And as you know you can print them out (in decimal) using printf
and %f
or %e
.
So what is it exactly that you want to do with your significands and/or exponents, in binary?
CodePudding user response:
You might want the stdlib function frexp
-- it will return the mantissa and exponent of any floating point number:
#include <math.h>
double val = ... something
int exp;
double mant = frexp(val, &exp);
printf("%f is %f * 2^%d\n", val, mant, exp);
Note that the mantissa will always have an absolute value in the range [0.5,1.0), so in your case you might want to multiply it by 2.
Unclear from your question, but you might also be asking how to print a number in binary ascii. The standard library does not provide any way to do that, so you need to do it manually, converting the number into a sequence of 0/1 digits. You can do that with frexp
and repeatedly doubling the number.
void binary_double(double v, FILE *fp) {
int exp;
v = frexp(v, &exp);
if (v < 0) {
fputc('-', fp);
v = -v; }
if (v == 0.0) {
fputc('0', fp);
} else if (v <= 1.0) {
if (exp <= 0) {
fputc('.', fp);
while (exp < 0) fputc('0', fp); }
while (v > 0 || exp > 0) {
v *= 2;
if (v >= 1.0) {
fputc('1', fp);
v -= 1.0;
} else {
fputc('0', fp); }
if (exp > 0) {
if (--exp == 0) fputc('.', fp); } }
} else if (v != 0.0) {
fputs("Inf", fp);
} else {
fputs("Nan", fp); }
}
A small modification to this could output in scientific notation.
CodePudding user response:
is there anyway to store this print value into a variable?
To print a double
as a string into a char
array for later processing, use snprintf()
and specify an ample precision for double
to print DBL_DECIMAL_DIG
digits. DBL_DECIMAL_DIG
is the digits needed to well distinguish all double
.:
#include <float.h>
// Make a big enough buffer
// - d . dddddd...dddddddd e - eeee \0
#define N (1 1 1 (DBL_DECIMAL_DIG-1) 1 1 4 1)
char buffer[N];
snprintf(buffer, sizeof buffer "%.*e", DBL_DECIMAL_DIG, some_double);
So far I only can print the binary in scientific notation using %e
This is strange as "%e"
is used to print a decimal, not binary, value in scientific notation.
Code could use "%a"
to print the double
with a hexadecimal significand and a power-of-2 exponent in decimal.
This is the closest to directly printing in binary available via the standard library.