Division of double and float in c [duplicate] - c

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

Related

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

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

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

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.

Floating Point Representation in C [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I'm not sure how floating point are represented in C, and how much of a precision someone could get.
In a c source file, I have the macro:
#define NUMBER 123.367
In the main function there are these 2 instructions:
float x = NUMBER;
printf("x is %f\n", x);
When I run it, I get:
x is 123.366997
Which is quite close to 123.367, but it kinda messes the purpose of the program.
Is there any way to round up x to the desired value? Or is this a flaw of floating point arithmetic-representation that can't be fixed?
You should use a double, not a float.
#include <stdio.h>
#define NUMBER 123.367
int main (void) {
float f = NUMBER;
double d = NUMBER;
printf("f is %f\n", f);
printf("d is %f\n", d);
return 0;
}
This will give you:
f is 123.366997
d is 123.367000
'double' uses 53 bits for precision, while a 'float' only uses 24.

C Language scientific notation problems [duplicate]

This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 7 years ago.
I have the following program:
float x = 3.e17;
printf("x = %f", x);
which gives the result:
x = 299999995292024832.000000
why is the result not 300000000000000000.000000?
#include <stdio.h>
#include <stdlib.h>
union
{
double d;
float f;
unsigned int ui[2];
} myun;
int main ( void )
{
float fx = 3.e17;
double dx = 3.e17;
printf("fx %f\n",fx);
printf("dx %lf\n",dx);
myun.f=fx;
printf("fx 0x%08X\n",myun.ui[0]);
myun.ui[0]++;
printf("fx %lf\n",myun.f);
myun.d=dx;
printf("dx 0x%08X 0x%08X\n",myun.ui[1],myun.ui[0]);
return(0);
}
(yes this is an incorrect/invalid way to use a union but it just happened to work)
fx 299999995292024832.000000
dx 300000000000000000.000000
fx 0x5C853A0D
fx 300000029651763200.000000
dx 0x4390A741 0xA4627800
wikipedia points out that single can handle up to 9 digits without a loss of precision and double 15-17. So right there is your answer, didnt necessarily need to do an experiment.
Because of limited float precision. Use double for more precision, but floating point is not always exact

Resources