wrong decimal values in c? [duplicate] - c

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 4 months ago.
i want to extract the decimal part of a float variable by substracting the whole part, the thing is i get a wrong value
#include<stdio.h>
int main(){
int k ;
float a=12.36,i;
k = (int)a;
i = a - k ;
i*=10;
printf("%f",i);
return 0;
}
well, the output is 3.599997 not 3.6 , is there a way to solve this ?
edit : i know it's because of the binary conversion, i m asking if there is a solution to get the right result, not the cause of it. ty for the replies anw.
edit 2 : sadly it's not a matter of display, i want the stored value to be 3.6 ( in this case) because i need it in other calculations.

Related

I think the below program should atleast run once as the condition is true or maybe i am getting it wrong. Please enlighten me [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
a very simple program but i can't figure out why it prints nothing. it should atleast run once.
int main()
{
float x = 1.1 ;
while ( x == 1.1 )
{
printf ( "%f\n", x ) ;
x = x-0.1 ;
}
return 0 ;
}
The decimal fraction 1.1 cannot be represented exactly using binary floating-point numbers (just like fraction 1/3 cannot be represented exactly using decimal floating-point numbers).
When you attempt to put 1.1 in type double, what you really store is 1.100000000000000088817841970012523233890533447265625
When you attempt to put 1.1 in type float, what you really store is 1.10000002384185791015625
Those values are not equal, so "x == 1.1" is false!
To make your program work, either use double
or use float:
````float x=1.1f; while (x==1.1f) ...
but don’t mix the types

What is wrong with this expression on the code? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);

using printf to print a floating number with varying precision [duplicate]

This question already has answers here:
Printf variable number of decimals in float
(3 answers)
Closed 6 years ago.
A simple question.
I want to print a floating point number with precision given input from the user, i.e. for num=2.34567 and prec=2, I should print 2.35 as the answer, and for prec=3, I should print 2.346. How can we achieve this? (prec is given input from the user during runtime).
Thanks in advance.
This is probably what you are looking for:
float num = 2.34567;
int prec = 3;
printf("%.*f", prec, num);

Log calls return NaN in C [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
I have an array of double:
double theoretical_distribution[] = {1/21, 2/21, 3/21, 4/21, 5/21, 6/21};
And I am trying to computer it's entropy as:
double entropy = 0;
for (int i = 0; i < sizeof(theoretical_distribution)/sizeof(*theoretical_distribution); i++) {
entropy -= (theoretical_distribution[i] * (log10(theoretical_distribution[i])/log10(arity)));
}
However I am getting NaN, I have checked the part
(theoretical_distribution[i] * (log10(theoretical_distribution[i])/log10(arity)))
And found it to return NaN itself, so I assume it's the culprit, however all it's supposed to be is a simple base conversion of the log? Am I missing some detail about the maths of it?
Why is it evaluating to NaN.
You are passing 0 to the log10 function.
This is because your array theoretical_distribution is being populated with constant values that result from integer computations, all of which have a denominator larger than the numerator.
You probably intended floating computations, so make at least one of the numerator or denominator a floating constant.

Percentage not showing in c++ with Two Dimensional Array and Loop [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 8 years ago.
This is my code, please help me out, percentage showing 0.00 instead of what i want.
I want to calculate percentage, as you will know this by code below...
#include<stdio.h>
#include<conio.h>
int main()
{
int marks[2][3]={80,70,50,90,50,60};
int total[2]={0,0};
float per[2]={0.0f,0.0f};
for (int x=0;x<2;x++)
{
for(int y=0;y<3;y++)
{
printf("[%d][%d]=%d\t",x,y,marks[x][y]);
total[x]=total[x]+marks[x][y];
per[x]=total[x]/300*100;
}
printf("total [%d]=%d",x,total[x]);
printf("\n\npercentage [%d]=%2.2f \n",x,per[x]);
putchar('\n');
}
getch();
return 0;
}
In the expression
total[x]/300*100
all involved values are integers, so the result is truncated before the assignment to the floating point array entry.
Change to e.g.
total[x]/300.0f*100.0f
Replace per[x]=total[x]/300*100; with per[x]=total[x] * 1.0f / 300 * 100;
You need to convert int to double / float before division to make sure you don't loose the precision because of truncation of integer division.
per[x]=total[x]/300*100; /* Assuming total[x] = 280 */
per[x]=280/300*100;
per[x]=(280/300)*100; /* Associativity is left-to-right */
per[x]=0*100;
per[x]=0;
You may also want to read integer division in C and operator associativity

Resources