Multiplication of float with int in c - c

#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.

Related

Why when using printf float and double show the same number of digits?

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

How to find empty space in a rectangle which has 2 circles in it?

I'm solving a problem in Toph . In this problem I've to find out the empty space of a rectangle which has 2 equal circles in it.
here is the problem
#include <stdio.h>
float pi=3.1416;
int main()
{
int i,t;
float r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%f",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2f\n",i,rest);
}
return 0;
Here is my solve. It returns a correct value for first test case but it fails to solve the second one.
What's the problem???
float pi=3.1416; is the cause of the problem. Under the math header file (#include <math.h>) there is a constant M_PI use it instead.
Edit:
Sorry, didn't read thoroughly, apparently the problem is in the floating point precision. If you change all float values into double it should work.
#include <stdio.h>
double pi=3.1416;
int main()
{
int i,t;
double r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%lf",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2lf\n",i,rest);
}
return 0;
}
Unlike 2 and 8, the reason double is more accurate is because float cannot represent 3.1416 as well as the input values:
3.1416 -> 3.1415998935699462890625
40.082 -> 40.082000732421875
85.8 -> 85.8000030517578125
There's simply not enough precision, (note that IEEE-754 float, which is overwhelmingly used for float, stores it in base-2.) Most probably, the later numbers were probably specifically generated in order to fail the test cases. If one wants to know more, Don’t Store That in a Float, and What Every Computer Scientist Should Know About Floating-Point Arithmetic.
The numerical constant is 1.7168, which is exact assuming their version of pi, (times r*r.) The best one could with single-point precision is 1.7167999744415283203125, which is off by 2.55584716796875E-8. With a double, it's 1.71680000000000010373923942097, off by 1.0373923942097E-16, plus the values input.

Double variable doesn't return the right values of a division [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
input example : 356
356/100, is suppused to be 3.56
But I'm getting 3.0000000000, I'm using ideone online compiler for C.
#include <stdio.h>
#include <math.h>
int main() {
int n;
double frac;
scanf("%d", &n);
frac = (n/100);
printf("%lf", frac);
return 0;
}
That's because here frac = (n/100); you are doing plain integer arithmetic (as n is declared as an int and 100 is interpreted as an int (any whole number is taken to be an int unless specified otherwise)). What you need to do is say explicitly that you want to do an arithmetic operation with digits after decimal point. One way is to use a cast: frac = ((double) n/100);
If you don't use the cast, the division will be performed as you expect, but then the digits after the decimal point will be dropped. Since frac is declared as a double, 0s would get tacked on to the end.
In C, the result of division of two integer numbers (e.g. int, short, long) is also an integer (it is counter-intuitive, but it is implemented this way for performance reasons). As a result, the result of 5/2 is 2 and not 2.5. This rule is only for integer numbers. So, if you need to get a floating-point result, at least one of the numbers in a division operation must be of a floating-point type.
In case of your code, if you use 100.0 instead of 100, you will get the desired result. Also you can use casts or define n as double.
This should work:
#include <stdio.h>
#include <math.h>
int main() {
int n; // You can define n as double but don't forget to modify scanf accordingly.
double frac;
scanf("%d", &n);
frac = (n/((double)100)); // Or, frac = (n/100.0)
printf("%lf", frac);
return 0;
}
You cannot call division using integers to be double without declaring it.
For example
int / int will result int.
Try declaring n as double
double n;
scanf("%lf", &n);
frag = n/100;
input data type is an integer.
just change it to double or float to fix this problem.
int main() {
double n;
double frac;
scanf("%d", &n);
frac = (n/100);
printf("%lf", frac);
return 0;
}

Inconsistent Floating Point Output

I would like to better understand floating point values and the imprecisions associated.
Following are two snippets with slight modifications
Snippet 1
#include <stdio.h>
int main(void)
{
float a = 12.59;
printf("%.100f\n", a);
}
Output:
12.5900001525878906250000000000000000000000000000000000000000000000000000000000000000000000000000000000
Snippet 2:
#include <stdio.h>
int main(void)
{
printf("%.100f\n", 12.59);
}
Output:
12.589999999999999857891452847979962825775146484375000000000000000000000000000000000000000000000000000
Why is there a difference in both the outputs? I'm unable to understand the catch.
In first case you have defined variable as a float and in second case you directly given the number.
System might be consider direct number as a double and not float.
So,I think may be it is because of system definition of float and double.
to get the consistent behaviour you can explicitly use floating point literal:
printf("%.100f\n", 12.59f);
In Snippet 1, your float gets cast into a double, and this casting causes a change in the value (due to the intricacies of floating point representation).
In Snippet 2, this cast doesn't happen, it's printed directly as a double.
To understand, try running the snippets below:
#include <stdio.h>
int main(void) {
double a = 12.59;
printf("%.100f\n", a);
return 0;
}
and
int main(void) {
float a = 12.59;
printf("%.100f\n", (double)a);
return 0;
}
Refer to this for more information: How does printf and co differentiate beetween float and double

How to separate float into an integer and a fractional part?

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

Resources