Why does the C code below output "Difference: 0.000000" ? I need to make calculations with many decimals in one of my university tasks and I don't understand this because I'm new to programming in C. Am I using the correct type? Thanks in advance.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
int main() {
long double a = 1.00000001;
long double b = 1.00000000;
long double difference = a-b;
printf("Difference: %Lf", difference);
}
I have tried that code and I'm expecting to get the result: "Difference: 0.00000001"
You see 0.000000 because %Lf prints a fixed number of decimal places, and the default number is 6. In your case, the difference is 1 in the 8th decimal place, which shows as 0.000000 when printed to 6 d.p. Either use %Le or %Lg or specify more precision: %.8Lf.
#include <stdio.h>
int main(void)
{
long double a = 1.00000001;
long double b = 1.00000000;
long double difference = a - b;
printf("Difference: %Lf\n", difference);
printf("Difference: %.8Lf\n", difference);
printf("Difference: %Le\n", difference);
printf("Difference: %Lg\n", difference);
return 0;
}
Note the minimal set of headers.
Output:
Difference: 0.000000
Difference: 0.00000001
Difference: 1.000000e-08
Difference: 1e-08
#include <stdio.h>
int main() {
long double a = 1.000000001;
long double b = 1.000000000;
long double difference = a-b;
printf("Difference: %.9Lf\n", difference);
}
Try this code. Actually, you need to specify to the compiler how much precision you need after the decimal point. Here the .9 will print 9 digits after the decimal point. You can adjust this value according to your needs; just don't exceed the range of the variable.
Related
I wanted to see the difference in how many digits i get when using float and when using double but i get the same results
#include <stdio.h>
int main()
{
float x=1.2222222222222222f;
printf("%f %d", x,sizeof(x)); // This is what it prints out 1.222222 4
return 0;
}
#include <stdio.h>
int main()
{
double x=1.2222222222222222;
printf("%f %d", x,sizeof(x)); // This is what it prints out 1.222222 8
return 0;
}
It prints out the same value even tho double is obviously double the size and should save more digits. What am i doing wrong?
sizeof returns size_t. To print size_t you need %zu instead of %d
If you want to see the real difference between float and double you need to print more digits using %.NUMBERf
Like:
#include <stdio.h>
int main(void)
{
float x=1.2222222222222222f;
printf("%.70f %zu\n", x,sizeof(x));
double y=1.2222222222222222;
printf("%.70f %zu\n", y,sizeof(y));
return 0;
}
Output:
1.2222222089767456054687500000000000000000000000000000000000000000000000 4
1.2222222222222220988641083749826066195964813232421875000000000000000000 8
It prints out the same value even tho double is obviously double the size and should save more digits.
When passing a float as a ... argument in printf(), it is first promoted to a double. "%f" prints that double to a rounded 6 places after the ..
Since the original values do not differ when rounded to 6 places after the decimal point, they appear the same.
What am i doing wrong?
Expecting the default precision of 6 is insufficient to distinguish.
Easiest to see different with "%a".
printf("%a\n", 1.2222222222222222);
printf("%a\n", 1.2222222222222222f);
0x1.38e38e38e38e3p+0
0x1.38e38ep+0
or with sufficient decimal places in exponential notation.
printf("%.*e\n", DBL_DECIMAL_DIG - 1, 1.2222222222222222);
printf("%.*e\n", DBL_DECIMAL_DIG - 1, 1.2222222222222222f);
1.2222222222222221e+00
1.2222222089767456e+00
I'm trying to split a number in C into two at the decimal point. For example, let's say the number is 1.5, then I want to split it into 1 and 0.5. How can I do this?
You can use modf() from math.h library:
#include <stdio.h>
#include <math.h>
int main () {
double value, fractional, integer;
value = 8.123456;
fractional = modf(value, &integer);
printf("Integral part = %lf\n", integer);
printf("Fraction Part = %lf \n", fractional);
return(0);
}
Sample from tutorialspoint.com
The C standard specifies a modf function, declared in <math.h>. This code puts the integer part of the double x in IntegerPart and the fraction part in FractionPart:
double IntegerPart;
double FractionPart = modf(x, &IntegerPart);
Each of IntegerPart and FractionPart will have the same sign as x.
I am willing to cast precise operations and for that purpose I need a way to
seperate a float number into an integer and a fractional part.
Is there any way for this?
There is a function included in math.h library called modf
With this function you can do just what are you trying to.
Example:
#include <stdio.h>
#include <math.h>
double ftof ()
{
double floating = 3.40, fractional, integer;
fractional = modf(floating, &integer);
printf ("Floating: %g\nInteger: %g\nFractional: %g", floating, integer, fractional); // when using printf, there are no floats
return fractional;
}
Output:
Floating: 3.40
Integer: 3
Fractional: 0.40
Note that using double in most of the cases is better than using float, despite that double
consumes twice the memory of float (4:8 bytes) hence the increased range and accuracy. Also in case you need more precise output from
bigger floating numbers when printing, you can try the printf() exponent format specifier %e instead of %g which only uses the
shortest representation of the floating decimal.
One other way using type cast.
#include <stdio.h>
#include <math.h>
void main()
{
float a = 3.4;
float a_frac = a - (int) a;
float a_int = a - a_frac;
printf("Number = %f, Integer = %f, Fraction = %f", a, a_frac, a_int);
}
A thought crossed my mind to separate them with some logic :
#include <iostream>
using namespace std;
int main()
{
double fr,i,in,num=12.7;
for(i=0;i<num;i++)
{
fr=num-i;
}
cout<<"num: "<<num;
cout<<"\nfraction: "<<fr;
in=num-fr;
cout<<"\nInteger: "<<in;
}
Hope this was what you were searching for:) .
#include <bits/stdc++.h>
using namespace std;
int main()
{
double n;
cin>>n;
double fr = n-((int)n);
cout<<"int "<<(int) n<<"\n";
cout<<"fraction "<< fr;
return 0;
}
#include <stdio.h>
int main(){
float var = 0.612;
printf("%f\n",var);
printf("%f\n",var*100);
return 0;
}
o/p
0.612000
61.199997
I found that for JavaScript, we have .tofixed() method.
How do we get a fix for this in C?
You can specify the precision when printing:
printf("%.3f\n", 100 * var);
Since the exact number you're having probably isn't representable in the float itself, there is no operation you can do on the number itself to "remove" the decimals, it's all a matter of how you choose to present the data.
The following code gives some odd results:
#include <stdio.h>
#include <float.h>
int main()
{
float t = 1.0;
float res;
float myFltMax = 340282346638528859.0;
printf("FLT_MAX %f\n", FLT_MAX);
res = FLT_MAX - t;
printf("res %f\n", res);
res = myFltMax - t;
printf("res myFltMax %f\n", res);
return 1;
}
The results are:
FLT_MAX 340282346638528859811704183484516925440.000000
res 340282346638528859811704183484516925440.000000
res myFltMax 340282356122255360.000000
So, if i subtract 1 from FLT_MAX the result is the same and if i subtract 1 from other big float, the result is greater than initial number.
I am using gcc version 4.7.2.
Thank you.
If you subtract 1 from myFltMax you don't get the difference greater than the initial number. You get the same number. Print myFltMax as well and you'll see that it's 340282356122255360 and not 340282346638528859.
Proof.
Basically, the compiler rounds your 340282346638528859 to the nearest value that can be represented in the floating point type and that happens to be 340282356122255360.