Not sure how to deal with repeating decimals in C [duplicate] - c

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 2 years ago.
int number = round(2/3);
printf("%i", number);
the output is
0
I expected to get 1 as 0.6 recurring should round up - i know for sure that the problem is with recurring decimals as they are always rounded down - I'm not sure how to round up in this kind of scenario.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void) {
double number = round(2.0/3.0);
printf("%0.f", number);
}
Output = 1
You can use double or float for rounding numbers

Without floating point calculations, the following will round to nearest using just integer division:
int a = 2;
int number = (a + 1) / 3; // = 1
In general, rounding a / b to the nearest integer is equivalent to truncating (a + b / 2) / b.

use double or float number. in definition and in printf
#include <stdio.h>
int main(void)
{
double number = (double)2/3;
printf("%.f", number);
}

Related

How does one make a floating point number stop rounding to an integer? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
How to divide integers and get a float in C
(2 answers)
Closed 2 years ago.
I'm trying to write a basic programme, which gets a user's input, divides by seven, and then returns the result.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int s = get_int("Input: ");
float f = (s / 7);
printf("%f\n", f);
}
Eg. if I input "8", I would expect 1.142857. But I instead simply get "1.000000". Why is this?
#include <stdio.h>
int main()
{
int s=4;
float f = ((float)s / 7);
printf("%f\n", f);
getch();
}
You just have to typecast the int to float. int/float division give you the floor of given integer whereas float/float division gives you float

Division of double and float in c [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
i believe that double have x2 precision of float .
in my calculator 10/3 is 3.3333333333333333333333333333333
when i do the following code :
#include <stdlib.h>
#include <stdio.h>
void main() {
double var = (double )10 / (double )3;
float var2 = (float )10 / (float )3;
printf("%f %f \n" , var , var2 );
}
i get the same number of digits after 3, :
3.3333333333333335 3.3333332538604736
why do i get the same number of digits? and why the value is different ? and how to do a division for double and float and get all the numbers and digits in c (like in my calculator) ?
By default printf will only print a limited number of digits. On my system it's 6 digits. On your system it seems to be 16 digits.
To print more digits do:
printf("%.60f \n%.60f \n" , var , var2 );
Output:
3.333333333333333481363069950020872056484222412109375000000000
3.333333253860473632812500000000000000000000000000000000000000
As you can see neither float nor double can print
3.333333333333333333333333333333333333333333333333333333333333
but - obviously - double is closer than float.
BTW: It's not void main() {. It shall be int main(void) {

Why the result is 1024 less instead of 1000 if we subtract 1000 from 2^63? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Could you explain why the difference is 1024 instead of 1000?
int main(void) {
unsigned long long int x = pow(2,63);
double y = pow(2,63) - 1000;
double z = 9223372036854775808.0 - 1000.0;
printf("%llu\n%f\n%f\n", x,y,z);
}
Output is:
9223372036854775808
9223372036854774784.000000
9223372036854774784.000000
Because among the floating-pointer numbers representable in type double, 9223372036854774784 happens to be the closest to the mathematically-correct result 9223372036854774808.
Let's inspect the respresentable neighborhood of your 9223372036854774784
#include <float.h>
#include <math.h>
#include <stdio.h>
int main()
{
double d = 9223372036854774784;
printf("%lf\n%lf\n%lf\n", nextafter(d, -DBL_MAX), d, nextafter(d, DBL_MAX));
}
On my platform the output is
9223372036854773760.000000
9223372036854774784.000000
9223372036854775808.000000
Which one would you pick? Your implementation decided to go with 9223372036854774784.

Does float and double datatype work in gcc compilers? [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 7 years ago.
I've written a code for the following program but the output seems to be wrong.
Question:
https://www.hackerrank.com/challenges/plus-minus
Code:
#include <stdio.h>
int main() {
int N,num,i,cp=0,cn=0,cz=0;
double fp,fn,fz;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&num);
if(num>0)
cp=cp+1;
else if(num==0)
cz=cz+1;
else
cn=cn+1;
}
fp=cp/N;
fn=cn/N;
fz=cz/N;
printf("%lf\n%lf\n%lf",fp,fn,fz);
return 0;
}
The Output comes as:
0.000000
0.000000
0.000000
Istructions:
fp=cp/N;
fn=cn/N;
fz=cz/N;
Are performed as integer division.
Change your code to:
fp=(double)(cp)/(double)(N);
fn=(double)(cn)/(double)(N);
fz=(double)(cz)/(double)(N);
you are doing an integer division which creates only integer results. If you want to calculate floating point results you need to perform floating point divisions.
int a = 1;
int b = 3;
int c = a / b;
// c is now 0 -> integer division
double i = 1.0;
double j = 3.0;
double k = i / j;
// k is now 0.3333333 -> floating point division
For correct result cast these expression's to double-
like this
fp=cp/N; // fp=(double)cp/N;
fn=cn/N; // fn=(double)cn/N;
fz=cz/N; // fz=(double)cz/N;
Working code
In previous case if cp(or cn or cz) is less than N then due to integer division you will get 0(fraction part will be discarded). Therefore m these casts .
Another method would be to use all variables as double .

C weird approximation on floating point [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
I have the following code:
#include<stdio.h>
int main(int argc, char const *argv[])
{
float min, max, step;
min = -0.85, max = 0.85, step = 0.002;
int rank = 3, total = 4;
float step1 = min + (max - min) * rank / total; // should be 0.425
printf("%f %.7g\n", step1, step1); // 0.425000 0.4250001
float step2 = min + (max - min) * (rank + 1) / total - step; //should be 0.848
printf("%f %.7g\n", step2, step2); // 0.848000 0.848
float noc = (step2 - step1 + step) / step; //should be 212,5
printf("%f %.7g\n", noc, noc); // 212.499985 212.5
int nol = 1200;
int result = (int)nol * noc; //should be 255000
printf("%d\n", result); // 254999
return 0;
}
This is a fragment of code isolated from a project I have to do. The final result should be 255000, but for some causes, it shows 254999. Can someone please explain me what happens in the process? I have read somewhere that multiplying a floating number with 10^k and then dividing back solves such problems, but in this case, due to the variable step varying from 0.000001 to 0.1, I can't actually use that (in the same way, I can't use a defined EPSILON). What else can I do?
Thanks in advance!
P.S.: I have used double and long double as well, but with same problems, only this time error propagates from a further decimal. I am using gcc 4.8.2, under Ubuntu 14.04.1.
Truncation vs. rounding.
Due to subtle rounding effect of FP arithmetic, the product nol * noc may be slightly less than an integer value. Conversion to int results in fractional truncation. Suggest rounding before conversion to int.
#include <math.h>
int result = (int) roundf(nol * noc);
the significant problem(s) are:
1) mixing floating point and double with integer math
--so the compiler promotes all the math to float (or double)
2) not all numbers can be expressed exactly in float
3) --the initialization of min, max, step are taking double literals
and converting them to float
--even double cannot express all values exactly
--some precision is lost when performing the conversion from double to float
4) this code excerpt: (rank + 1) / total is always = 1
--(although the many conversions may result in being 'not exactly' 1)
5) argc and argv are not referenced in your code.
--this, given that all warnings are enabled, will rise two warnings
at compile time about unused parameters
6) this line in your code is not correct syntax
--(although the compiler might not complain) #include<stdio.h>
--it should be #include <stdio.h>
--sometimes spaces count, sometimes they dont

Resources