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

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.

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

I made a simple C program to receive a float as input from user and display it in screen. But the value changes by a small amount while printing [duplicate]

This question already has answers here:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 1 year ago.
I made a simple C program to get a float as input from the user using scanf() function and dispay it using printf() function. But when the input number is quite large, the output deviates slightly. Why might that happen?
For example: if I give input as 2345, out put will be as expected. But if I give the output as 1234567898, the output will be unexpected.
#include <stdio.h>
int main()
{
float number;
scanf("%f", &number);
printf("The number %f", number);
return 0;
}
Single precision floating point (float) is good for approximately 6 significant decimal digits. double allows 15 significant figures. 1234567898.0 has 10 significant figures. Only the most significant first 6 digits can be relied on.

Deriving decimal numbers in float input sometimes minus the value by 1 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am trying to separate the numbers in a floating input (xx.yy) and extract them out.
Not so sure if this is the online compiler fault but for some reasons, there are times when I run my following code, the decimal places that I extract seemingly -1 in value.
I am using this online compiler (https://repl.it/languages/c).
#include <stdio.h>
int main()
{
int whole_num = 0;
int dec_num = 0;
float u_input = 0.0;
printf("Input in a float: ");
scanf("%f", &u_input);
whole_num = (int) u_input;
printf("1. Whole number is %d\n", whole_num);
dec_num = (u_input - whole_num)*100;
printf("2. Decimal number is %d\n", dec_num);
return 0;
}
For example, there are times when I input in 99.95, instead of expecting 95 for my dec_num, it gave me 94. Then again, if I used another input such as 123.95 it returns me 95 instead. And as a result, if my code is giving me the -1 result, it will fails in other parts of my code.Decimal
I can't find out if it is an error with the way I have written my code and/ or if it is an issue with the compiler (also tried it with other online compilers which seemingly giving me similar results)
Can anyone share any insights to me on this? Or if there is a proper method that I will get the decimal number I expected without the -1?
Floating point numbers are represented using binary notation (see https://en.wikipedia.org/wiki/IEEE_754).
As such, decimal numbers that do not have an exact binary representation are stored as an approximation to the desired values.
For example, a value like 1.25 has an exact binary representation (5*(2^-2)), while numbers like 1.3 do not and need to be apprximated.
This website offers a good visualization of this phenomenon.

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

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

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

Resources