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

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

Related

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

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);

How does printf work? Why is the output different? [duplicate]

This question already has answers here:
What happens to a float variable when %d is used in a printf?
(4 answers)
Closed 6 years ago.
Consider these two programs:
#include <stdio.h>
int main()
{
int z = 6.4;
printf("%d %d", z, 6.4);
return 0;
}
Output is 6 -1717986918.
#include <stdio.h>
int main()
{
int z = 6.4;
printf("%d %d", 6.4, z);
return 0;
}
Output is -1717986918 1075419545.
Why does the output vary like that? When z was first then the output printed correct z value which was 6 as z is an integer, but when I reversed the arguments, the output is a different value. Why does it behave like that?
You store 6.4 in an integer which will result in z being 6. The 6 gets passed to printf which is interpreting the passed value as an decimal, thus the correct output was displayed.
But if you pass a floating point number directly to printf, and specifying the format %d, it will also interpret it as an decimal which will result in the decimal interpretations of that float.
This interpretation happens on a bit level. Float is internally stored in a rather complex format. But not like an integer.
Now those bits get interpreter as an integer, and obviously the result is unreadable for humans. But the program could actually interpreted that integer as an float and reverse this action.
This site will demonstrate how floats are stored on the bit level.
And on this website you can convert the bits from the previous site to an signed integer which should output the negative number you specified in the question.

Why is the same floating-point constant printed differently in Fortran and in C? [duplicate]

This question already has answers here:
Different precision in C++ and Fortran
(2 answers)
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Printing constant 0.8804418 in Fortran
program hello
print *, 0.8804418
end program hello
gives 0.880441785 while the C version
#include <stdio.h>
int main() {
printf("%.10g", 0.8804418);
}
prints 0.8804418 on the same system (Linux x86-64 with gfortran and gcc). Why is the output different? Note that increasing precision in printf doesn't change output.
This is not a duplicate of Is floating point math broken? or similar. This question is specifically about difference in Fortran and C representation (or formatting).
By default, Fortran's REAL number constants are single-precision; In C, however, floating point literals have double precision.
When you translate 0.8804418 to single precision and then print it as a double in C, you get 0.8804417849 printed (demo)
float x = 0.8804418f;
printf("%.10g\n", x);
Fortran's printout appears to be the same number rounded up.
Fortran's syntax for double-precision REAL numbers uses suffix d:
print *, 0.8804418d+0
This prints 0.88044180000000005 (demo).

Floating point number is rounding off in C [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 6 years ago.
i started learning c. Today, while i am working on a program, i found an interesting thing and i made another small program (similar to my issue) to check it.
#include<stdio.h>
int main(void)
{
float num1=867.0;
float num2=.6921;
printf("sum = %.4f \n",num1+num2);
return 0;
}
if i run the above program, i am getting 867.6921 as the answer. but if i changed the .4%f to %f the answer is changing to sum = 867.692078. why's the change in the output? Also, is there any way that i can get the answer without using the .4%f?
but if i changed the .4%f to %f the answer is changing to sum = 867.692078
printf rounds the result to that many digits after the floating point you specified, the default is .6. While rounding, it uses the current floating point rounding mode, the default is round to the nearest.
The float type has about seven digits of precision (can be anywhere from 6-9 digits). One alternative is to use g instead of f. This fixes the number of precise digits in the number, not just those after the decimal.
printf("sum = %.7g \n",num1+num2);
This produces output
sum = 867.6921
and will always give seven digits of precision for any input. Number formats would be like :
0.000000E+07
0000000
000000.0
00000.00
0000.000
000.0000
...etc

Why does this C computation involving exponents produce the wrong answer? [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 9 years ago.
I am trying to implement the below formula on C
Here is my code:
int function(int x){
return pow(10, (((x-1)/(253/3))-1));
}
int main(void){
int z = function(252);
printf("z: %d\n",z);
return 0;
}
it outputs 10. However a calculator outputs 94.6.
could anyone explain me what I am doing wrong?
Note that in this line
(((x-1)/(253/3))-1))
You are dividing the integer value x - 1 by an integer value 253 / 3. This will truncate the value to an int, meaning that you'll be raising an integer power to an integer power.
To fix this, try changing this expression to
(((x-1)/(253.0 / 3.0))-1))
This now will use doubles in the expression, giving you the value you want.
Hope this helps!
Adding to that, integers do not give you the decimal part of numbers, so a number like 3.5 will get cut down to 3 using integer. To fix this double is the way to go. In other words 3 is different than 3.0

Resources