Why does this C computation involving exponents produce the wrong answer? [duplicate] - c

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 9 years ago.
I am trying to implement the below formula on C
Here is my code:
int function(int x){
return pow(10, (((x-1)/(253/3))-1));
}
int main(void){
int z = function(252);
printf("z: %d\n",z);
return 0;
}
it outputs 10. However a calculator outputs 94.6.
could anyone explain me what I am doing wrong?

Note that in this line
(((x-1)/(253/3))-1))
You are dividing the integer value x - 1 by an integer value 253 / 3. This will truncate the value to an int, meaning that you'll be raising an integer power to an integer power.
To fix this, try changing this expression to
(((x-1)/(253.0 / 3.0))-1))
This now will use doubles in the expression, giving you the value you want.
Hope this helps!

Adding to that, integers do not give you the decimal part of numbers, so a number like 3.5 will get cut down to 3 using integer. To fix this double is the way to go. In other words 3 is different than 3.0

Related

C pow() function printing gigantic numbers? [duplicate]

This question already has answers here:
c++ pow(2,1000) is normaly to big for double, but it's working. why?
(3 answers)
Closed 1 year ago.
sorry if this is a silly question, I am relatively new to C programming. Thus I have probably misunderstood something fundamental about variables/overflows.
I made this simple program and I can't explain the result
#include <stdio.h>
#include <math.h>
int main() {
double number = pow(2, 1000);
printf("%f\n", number);
}
The result of that (compiled with gcc) is a humongous number that should never fit in a variable of type double: 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376.000000
Usually when I try to assign a constant that is too large for it's type the compiler gives me an error.
So my question is: What is this number? Why am I not getting any errors? Is the variable number actually storing this value? Plainly, what's happening?
Doubles are not stored like integers. This page explains how double are representated in memory
Doubles contain 1 byte of sign, 11 bits for the exponent and 53 bits for the sigificand precision. Thus you can store numbers up to 1.7*10^308. Thus, your number cand be represented in a double (although with a limited precision). When printing it with %f, you just get its numerical value (approximated).
In C, double type can fit more than 300 decimal digits! A double is stored in 8 bytes, holding a number in the range 2.3E-308 to 1.7E+308 (15 decimal places accuracy).
Reference: https://www.tutorialspoint.com/cprogramming/c_data_types.htm

Why do the square of input decreases by one when using pow function? [duplicate]

This question already has answers here:
unusual output from pow
(3 answers)
pow() function in C problems [duplicate]
(1 answer)
Why the result of pow(10,2) 99 instead of 100? [duplicate]
(1 answer)
What's wrong with long? Why is subtracting 1 automatically?
(1 answer)
Closed 2 years ago.
Here is a simple C program, which accepts a number from the user and results it's square.
#include <stdio.h>
#include <math.h>
int main()
{
int number;
int result;
printf("\nEnter the number\n");
scanf("%d",&number);
result=(pow(number,2));
printf("\nThe result is %d\n",result);
return 0;
}
The problem is, whenever i enter 5,25,26 etc as input, the output is 24,624,675 i.e. it decreases by 1 and this does not happen with all numbers. I am using CodeBlocks IDE. I figured out a fix for this problem but I want to know what is happening behind the scene, which is causing this error.
From this post, Sourav tells us:
pow() takes double arguments and returns a double.
If you store the return value into an int and print that, you may not get the desired result.
As for an explanation on why pow() is returning 1 less that you expect, see this post. Specifically:
pow() works with double numbers. These represent numbers of the form s * 2^e where s is a 53 bit integer. Therefore double can store all integers below 2^53, but only some integers above 2^53. In particular, it can only represent even numbers > 2^53, since for e > 0 the value is always a multiple of 2.

Float variable comparison fails [duplicate]

This question already has an answer here:
Why does this program gives no output for float and double datatypes?
(1 answer)
Closed 7 years ago.
The below program seems to look like it will run for one time but when i run in Turbo C , the output is nothing.
Can any one explain this ?
#include<stdio.h>
int main()
{
float x=1.1;
while(x==1.1)
{
printf("%f \n",x);
x=x-0.1;
}
return 0;
}
By default, floating point numbers are stored as type 'double'. So, a comparison on float and double value is done.
I think,
if(x==1.1f)
it should solve the problem.
And also FLT_EPSILON is the smallest difference between two floating point numbers for them to be same.
if( abs(x-1.1f) <= FLT_EPSILON)
should work
You can re-write your loop condition as follows-
while((1.0009<x)&&(x<1.10001))
As because x=1.1 in this x is never exact 1.1. At higher decimal places it can have different value.
You can see here at higher decimal places what are values and working example for your code -https://ideone.com/IgrLAY

Not getting expected value when running a program [duplicate]

This question already has answers here:
What decides what datatype that will be used to store the temporary value in?
(3 answers)
Closed 7 years ago.
int a=10;
float b;
printf("the no\n");
b=((10-1)/12)*50;
printf("b value is %f",b);
return 0;
But when I calculate the b value in scientific calc we get b=40. And my question is why it shows b=0 when I run my code
In the calculation, the expression ((10-1)/12) is a division of an integer by an integer. The first integer evaluates to 9, and since 9/12 is less than 1, 9/12 evaluates to 0. This 0 is then multiplied by 50 to give b = 0.
To make the division act like a float, make one of the constants in the division portion a float. You can do this several ways the shortest of which is to add "f" as a suffix to one of the numbers, e.g. use 10F instead of 10, to get ((10F-1)/12). This makes the subtraction a floating point operation, which make the division and then multiplication of your original expression floating point operations as well. This should then give you the expected (float) result.
There will be a loss of data when you try to perform integer division. As already explained,
b=((10-1)/12)*50;
will yield 0 because :
((10-1)/12)*50; = (9/12)*50; = (0)*50; = 0;
To prevent this data loss, you can do the following:
((10-1)/12.0)*50;
Basically, by 12.0, it will be read as a float, so appropriately, floating point division will be performed.

number hold by double container is greater than float even if values are equal [duplicate]

This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 9 years ago.
main()
{
float f=0.7;
if(0.7>f)
printf("Hi");
else
printf("Hello"):
}
When I compile this program output comes out to be Hi.
Can someone please explain the scenerio how 0.7>0.7 is true? Is this due to fact that 0.7 used in if statement is a double and f is a float?
Even if it is double still its value is 0.7 difference created is just that in case of double it is stored in 8 byte while in case of float it is stored in 4 byte. But I think it does not matters how big is your container when the value stored in container is equal. So 0.7 can never be greater than f. So according to me it should result in Hello. then why Hi is the output?
The container does makes the difference here. The idea is how these values are stored and then retrieved. If you use a number that can be converted back to exact decimal value. You may see the difference.
Use this and see the output:
float f=0.7f;
If you define float f = 0.7, it is stored as manttisa and exponent. It depends on the precision. So in this case .7 will be less than .7. If you define 0.7f it will be equal to 0.7.

Resources