About the division of floating point numbers in C language。 [duplicate] - c

This question already has answers here:
What happens when I use the wrong format specifier?
(2 answers)
Closed 5 months ago.
I am a university student. When learning C language, I encountered the problem of floating point division. Can anyone help me to see why the results are different every time I run it. thank you very much.( I am running in a Linux)
My English is not good, hope you can understand the meaning of my question。
#include <stdio.h>
int main()
{
double a = 1;
double b = 2;
printf("%d\n",b/a);
return 0;
}

You're using the wrong format specifier.
The %d former specifier for printf expects an int as an argument, but you're passing in a double. Using the wrong format specifier triggers undefined behavior.
You should instead use %f which is for printing a double.
printf("%f\n",b/a);

Related

Why is the result of -5/2 being printed as 0? [duplicate]

This question already has answers here:
What happens when I use the wrong format specifier?
(2 answers)
Closed 1 year ago.
Can you tell why this statement in C gives 0 as output
printf("Hello %f",-5/2);
Whereas
printf("Hello %d",-5/2);
is giving output as -2
Division of two integers produces a integer result (int in this case). The %f format specifier expects an argument of type double. Using the wrong format specifier for a given argument triggers undefined behavior which in this case gives you an incorrect result.
If you want a floating point result, at least one of the operands to / must have a floating point type, i.e.
printf("Hello %f",-5.0/2);

What does the constant E do in the c language [duplicate]

This question already has answers here:
What is E in floating point?
(4 answers)
Scanf/Printf double variable C
(3 answers)
Closed 4 years ago.
When I run the code below
int main(int argc,char *argv[]) {
double n = 2E-1;
printf("%d",n);
}
When I run the code it prints a weird number instead of 0.2(2E-1).
What does the constant E do in the c language
The documentation for floating-point constants is here.
The form 1e2 or 1E2 means 1 times ten to the power of 2, and you're perfectly correct that 2E-1 means 2 times ten to the power of -1, or 0.2.
It's based on the scientific E notation linked by Eugene.
When I run the code it prints a weird number
That's just because you used the wrong format specifier. These are documented here, and you can see you should use one of eEfFgG for doubles.
In C language, for double, the format specifier is %lf
So, if you use %lf then it will print n as 0.200000
And if you use %g or %G (as supported data types are float, double) then the output will be 0.2

Expected value not obtained: why? [duplicate]

This question already has answers here:
printf specify integer format string for float
(7 answers)
Closed 5 years ago.
I wrote this very simple and short code, but it doesn't work: when I compile and execute the returned value from the function calculateCharges() is 0 when I'm expecting 2.
Can anybody explain why, please?
#include <stdio.h>
#include <stdlib.h>
float calculateCharges(float timeIn);
int main()
{
printf("%d", calculateCharges(3.0));
return 0;
}
float calculateCharges(float timeIn)
{
float Total;
if(timeIn <= 3.0)
Total = 2.0;
return Total;
}
There are at least three problems here, two of which should be easily noticeable if you enable compiler warnings (-Wall command-line option), and which lead to undefined behavior.
One is wrong format specifier in your printf statement. You're printing a floating point value wirh %d, the format specifier for signed integer. The correct specifier is %f.
The other is using uninitialized value. The variable Total is potentially uninitialized if the if statement in your function isn't gone through, and the behavior of such usage is undefined.
From my point of view, it's likely the wrong format specifier that caused the wrong output. But it's also recommended that you fix the second problem described above.
The third problem has to do with floating point precision. Casting values between float and double may not be a safe round-trip operation.
Your 3.0 double constant is cast to float when passed to calculateCharges(). That value is then cast up to a double in the timeIn <= 3.0 comparison (to match the type of 3.0).
It's probably okay with a value like 3.0 but it's not safe in the general case. See, for example, this piece of code which exhibits the problem.
#include <stdio.h>
#define EPI 2.71828182846314159265359
void checkDouble(double x) {
printf("double %s\n", (x == EPI) ? "okay" : "bad");
}
void checkFloat(float x) {
printf("float %s\n", (x == EPI) ? "okay" : "bad");
}
int main(void) {
checkFloat(EPI);
checkDouble(EPI);
return 0;
}
You can see from the output that treating it as double always is okay but not so when you cast to float and lose precision:
float bad
double okay
Of course, the problem goes away if you ensure you always use and check against the correct constant types, such as by using 3.0F.
%d will print integers.
Total is a float, so it will not work.
You must use the proper specifier for a float.
(You should research that yourself, rather than have us give you the answer)

C: strtof wrong conversion by a unit [duplicate]

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.

printf function in c [duplicate]

This question already has answers here:
float to int unexpected behaviour
(6 answers)
Closed 6 years ago.
here are part of my code.
float a = 12.5;
printf("%d\n", a);
printf("%d\n", (int)a);
printf("%d\n", *(int *)&a);
when I compile in windows, I got:
0
12
1094713344
and then, I compile in linux, I got:
-1437851864
12
1094713344
-1437851864 will be changed every time I excuted it.
my question is: in how does the "printf" function works in linux
It works very well, but why are you passing the wrong sort of data to it? The %d specifier expects and int, but you're passing something else. Bad idea.
If float and int are differently sized across the varargs barrier, this is undefined behavior. And since float is typically promoted to double with varargs calls, if your int is smaller than your double this will break.
In short, this is really bad and broken code. Don't do this.
To print a floating point number in C, you should do:
float a = 12.5;
printf("%f\n", a);
As has been mentioned, passing arguments with types not matching the format string invokes undefined behaviour, so the language standard doesn't place any restrictions on what
float a = 12.5;
printf("%d\n", a);
actually does.
To find out what it does, you'd need to analyse your implementation, or at least the assembly the compiler produced for that code.
A common way for translating that code is to pass the promoted (to double) float argument in a floating point register and tell printf how many arguments are passed in floating point registers. But since the format tells printf to look for an int, it doesn't look in a floating point register for it, but in another register. So the printed value would be whatever happened to be in that register when printf was called.

Resources