This question already has answers here:
Floating point does not print accurately [duplicate]
(4 answers)
Closed 8 years ago.
I want to convert a char like "12345678901234567890.123 " to double,
so I use sscanf(str, "%lf", &d),
and I print it use printf("%20.3lf", d);
But I get the result is "12345678901234567178.000",
How can I fix it?
You probably can't, since you're trying to store a number with more digits of precision than will fit in your machine's double data type.
You need to use a big-number library, that "manually" computes with arbitrary number of digits and thus can go beyond the limits of your machine's basic data types.
Oh, and "12345678901234567890.123" is not "a char", it's called "a string".
Related
This question already has answers here:
How to control the number of exponent digits after 'e' in C printf %e?
(3 answers)
Closed 3 years ago.
When I use this:
printf("%e", 2000.0);
I get output 2.000000e+003 which is correct but very ugly. I then tried like this:
printf("%.1e", 2000.0);
Which outputs 2.0e+003 which is far better. The question is how to remove the 2 padding zeroes in the exponent so that the output is 2.0e+3. Is there a specific printf specifier for that or do I have to write a function manually that analyzes the floating point number and prints it the way I want (I'd hate to have to do that)?
The standard C features for printf do not provide any means of controlling the number of digits in the exponent. If you want a different format, you will have to write your own code or obtain code from somewhere else.
(The %a conversion does use only as many digits in the exponent as needed, but it prints the value with hexadecimal.)
This question already has answers here:
How to Write a Boolean Expression Evaluator in C?
(9 answers)
Closed 5 years ago.
A string is input as a fraction, how can I convert this string to float?
In the below code it always show the first number only without any calculations i.e : 2.00
int main (void)
{
string fraction = "2/4";
float sum = atof(fraction);
printf("sum : %.2f\n",sum);
return 0;
}
First of all, your assumption is wrong.
A string like "2/4", is "2/4", it's not "0.5", i.e., it does not evaluate itself to produce the result of the expected expression.
That said, atof() family is not safe (at least, poor with reporting / handling error conditions, which, in your particular case, would have been very useful, should you have checked the return code), please use strtod() and family.
You cannot do this directly. With atof you e.g. change "2.034" to 2.034 (f), but not an expression (with a division in your case).
Instead, if you know there will be always a divider symbol, search for the divider symbol, than extract the left and right part, use atoi (string to integer), and divide the two integers (after casting to float).
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Why is scanf taking the wrong input for large float numbers? [duplicate]
(1 answer)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm having problems with strtof conversions, some values it works well but another ones it returns +1 or -1 of the value. My program reads the value from a file but for simplicity I have tried putting directly the value to strtof and it has the same behavior.
My simple code:
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("%f", strtof("17309217",NULL));
}
It returns
17309216.000000
The same value that it gives if I convert "17309216" or "17309215".
I have been doing some more tests and it seems a "pattern", "17309214" works well but "17309213", "17309212" and "17309211" all return "17309212". After this "17309210" works well but "17309209", "17309208" and "17309207" return "17309208", and so on...
What is the explanation for this behaviour? Now I convert the string with other methods, I don't need a method to convert it. I only want to know why strtof is doing this, so I can decide where I can use it and where can't.
I have been reading some other questions asked related to wrong conversions but all are related to the decimal rounding, not an entire unit.
Thanks in advance,
You're converting to a 32-bit floating point which may not have the precision to handle that many digits.
This question already has answers here:
How to represent FLOAT number in memory in C
(11 answers)
Closed 7 years ago.
I was reading the way, how integers are stored in variables in c, that the last bit is used for the sign of the integer and the remaining bits are used to store the number.
But if we take a double variable and the long int variable in c, both has a size of 4 bytes but the float can store the very huge numbers up to a range of 1038 but long int of same size cant store such huge value.
I want to understand the mechanism which is used in storage in float.
The C language does not require any specific representation for floating point numbers.
Today, most C implementations are using IEEE floating numbers (exceptions are unusual, perhaps some Series Z mainframes from IBM).
Read http://floating-point-gui.de/
The complete explanation can be found here . Basically, the number is not fully stored, only approximately. The 32 bits are used to store as much precision as possible.
This question already has answers here:
How can I convert an integer to a hexadecimal string in C?
(7 answers)
Closed 8 years ago.
I tried to convert dec to hex. For example convert 255 to hex.
opa = 255.ToString("X");
gives me error:
error: invalid suffix "ToString" on floating constant
I spent lots of time to convert, but couldn't find right way.
You're asking about the wrong language. C does not support the dot operator on integers. To do this in C, you need to print it to a string like so.
char numstr[10];
sprintf(numstr, "%X", 255)