This question already has answers here:
return value of pow() gets rounded down if assigned to an integer
(4 answers)
Closed 5 years ago.
I'm new with C programming, and I've written this code as I've been asked to do something using mainly printf() and scanf(). I know there could be better ways to handle this, which I'll need to learn soon, but anyways, for now this is what I have:
int add1, add2, exponent, exponent_result, multiplier, parenthese, product, sub, total;
printf("Let's try a slightly more complex calculation, in which we'll use an exponent.\n\n");
printf("Type 5 whole numbers or integers.\n\n");
scanf("%i %i %i %i %i", &add1, &add2, &exponent, &multiplier, &sub);
printf("Out of the numbers you typed, we're going to make this operation: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
exponent_result = pow(add2, exponent);
parenthese = add1 + exponent_result;
product = parenthese * multiplier;
total = (add1 + exponent_result) * multiplier - sub;
printf("Per PEMDAS, the correct order of operation is Parentheses, then Exponents, then Multiplications and Divisions, and finally Additions and Subtractions.\n\n");
printf("Therefore: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
printf("...is equal to: (%i + %i) * %i - %i\n\n", add1, exponent_result, multiplier, sub);
printf("...is equal to: %i * %i - %i\n\n", parenthese, multiplier, sub);
printf("...is equal to: %i - %i\n\n", product, sub);
printf("...is equal to: %i", total);
If you run this code, you'll realize that the output for exponent_result, which is calculated using the pow() function, always has 1 subtracted from it. For instance, if exponent_result is supposed to be the result of 5^3, the result for that will be 124 instead of 125.
What am I doing wrong?
Fyi, I have this at the beginning of my file.
#include <stdio.h>
#include <math.h>
pow is evaluated in floating point arithmetic, and is probably implemented as pow(x, y) = exp(y * log(x)).
This can cause the result to "go off": you probably get a value just shy of 125, which is truncated to 124 when the double return type from pow is converted back to an int.
The simplest remedy is to build your own pow function for integral arguments. See The most efficient way to implement an integer based power function pow(int, int)
Related
int num1, num2;
double average;
average=(double)(num1+num2)/2;
printf("average: %d", average);
My test printf shows average as: 0
This is maybe too easy, but I can't see it. My inputs are both "int" and the average is "double" but somehow it is not calculating right ?
You're using the wrong format specifier to printf.
The %d format specifier expects an int argument, but you're passing a double. Using the wrong format specifier invokes undefined behavior.
To print a double, use %f.
printf("average: %f\n", average);
No need to modify the statement average=(double)(num1+num2)/2; to get expected result inside printf use %f instead of %d
1st (num1+num2) is performed, result of this is of integral type. lets say 15. Next when you do (double)15/2 result is of floating type which is 7.500000.
from previous step average = (double)7.500000 average holds 7.500000 but since you printed in %d you are getting 0 as its undefined behavior. instead use %f
Here is the working one
int main() {
int num1 = 7, num2 = 8;
double average;
average = (double)(num1 + num2)/2;
printf("average: %f\n", average);
return 0;
}
int num1, num2;
double average;
average=(num1+num2)/2.; // Using a decimal point forces 2 to be a double.
printf("average: %f\n", average); // Use the correct specifier for double: %f
You should use printf("average= %f",avearge); instead of using "%d" to print the average value.I think it will solve your issues...
Integer division yields an integer result: 1/2 == 0, 5/3 == 1, etc. If you want a floating point result, at least one of the operands must be a floating-point type: 1/2.0f == 0.5f, 5/3.0 == 1.6667, etc.
So, you'll want to divide your sum by 2.0, not 2:
average = (num1 + num2)/2.0;
Secondly, you need to use the %f format specifier for floating-point output:
printf( "average: %f\n", average );
If you want a floating point result, at least one of the operands must be a floating-point type
This is my solution:
average = (num1 + num2)/(double)2;
printf ("Value of 1/2 is: %.4f\n", (double)(1/2));
// Value of 1/2 is: 0.0000
printf ("Value of 1/2 is: %.4f\n", (1/(double)2));
// Value of 1/2 is: 0.5000
#include<stdio.h>
main()
{
int m1, m2, m3, m4, m5, per;
printf("Enter marks in five subjects");
scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);
per=(m1+m2+m3+m4+m5)/500*100;
if(per>=60)
printf("First division");
else if(per>=50)
printf("Second division");
else if(per>=40)
printf("Third division");
else
printf("Fail");
}
Enter marks in five subjects 100, 100, 10, 50, 60
After giving this input printf showing Fail. But it will be First Division.
What is the problem in this program?
You are performing integer division
per=(m1+m2+m3+m4+m5)/500*100;
change it to
per = ((float)(m1 + m2 + m3 + m4 + m5)) / 500 * 100;
and since the maximum value of each mn is 100 so the maximum value of their sum is 500, if it's less than 500 integer division yields always 0.
Iharob is right - the problem is in making integer division (i.e. int_operator / int_operator), and only after that multiplication works.
Modifications of Iharob’s solution can be also:
per = (m1 + m2 + m3 + m4 + m5) / 500.0 * 100; // now 500.0 is of double type
But assigning float (or double) values to integer variables is not always good idea because int has smaller range. So it is better to optimize your expression, e.g.:
per=(m1+m2+m3+m4+m5)/5;
Also you can change type of per to float (or double), or decide what you want from the result (rounding / separation integer part from remainder / just integer part)
I'm currently learning the C language and I'm having trouble in the double multiplication topic.
I need to to print the original value and then the 2*value of the double.
double num = 34.39;
printf("Original value = %d, 2x original value = %d", num, num*2);
How do I make it so that the 2x value will be really 2x the original value?
Your multiplication is not the problem.
Your printf format string is. %d is not for floating-point values, but for integers, so you're seeing nonsense resulting from your broken contract with the compiler.
double num = 34.39;
printf("Original value = %lf, 2x original value = %lf", num, num*2);
%d - for int
You must use the "%f" for printf
I tried to convert a number n-based to decimal with C. I have this code :
scanf("%d", &n);
scanf("%d %d %d %d", &n1, &n2, &n3, &n4);
dec = ( n1*pow(n,3) + n2*pow(n,2) + n3*pow(n,1) + n4*pow(n,0) );
It works fine until I enter n=10 & n1 n2 n3 n4=0 2 5 4.
The result should be 254, but the terminal showed 253. It turns out that starting from 200-999, the dec decreased by 1. However when I input 1000, the dec=1000. How can I fix this?
Most likely you have declared dec as an int while the function pow returns a double value. Here double is converted to an int an is rounded.
change to
double dec = ( n1*pow(n,3) + n2*pow(n,2) + n3*pow(n,1) + n4*pow(n,0) );
printf("%f",dec);
and the output will be
254.000000
While the code you have provided works fine on some implementations, example here, you could preferably not use the pow function since you are working with integers and non-negatives as power, and define your own intpow function. A naive implementation of that would be:
int intpow(int n, unsigned int power)
{
int result = 1;
while (power)
{
result *= n;
power--;
}
return result;
}
It just might be that that whole sum, which is a sum of doubles, turns out to be less than the number you expect to see by, say, 0.000013, of which the digits after the decimal point are discarded when being converted to get assigned to that int dec.
You could check the double result by changing the type of dec to double and printing it with the format specifier "%f". If that's the case, my recommendation would be a proper solution.
You may also try and think of better ways of base conversion, because there is one which doesn't require a (int)pow at all.
In my quest to learn C I've come across a task which is causing me a few problems. I need to make an equation for the approximate value of the formulae n!, which can be described as:
n! = n^n*e^(-n)*sqrt(2(2*n+1/3)*PI), however I simply cannot get my values to corrospond with the actual value. 5! = 120ish
I can get a value of some 148ish
Can't figure out where my code is wrong:
#include <stdio.h>
#include <math.h>
#define PI 3.14156
#define E_CONST 2.7828
int main ()
{
double num;
double calc, first, second, third, fourth;
printf("Give an int: ");
scanf("%lf", &num);
first = pow(num , num);
second = pow(E_CONST, -num);
third = (2 * num + 1/3);
fourth = sqrt(2*third*PI);
//calc = first * second * fourth;
calc = pow(num, num) * pow(E_CONST, -num) * sqrt(2*(2*num+(1/3))*PI);
printf("Input: %f", num);
printf("1: %.2f\n2: %.10f\n3: %.8f\n4: %.2f\n", first, second, third, fourth);
printf("\nInt was: %.2f\n\nApproximate number: %.5f", num, calc);
return 0;
}
Feel like i have tried everything. The code is a bit messy, but it's because I've scrambled so much with it now.
3.14156 is a bad value for PI: it's better to use 3.1416, or 3.14159, or 4 * atan(1), or, for POSIX implementations, M_PI.
2.7828 is a very bad value for e: it's better to use 2.7183, or exp(1), or, for POSIX implementations, M_E.
1/3 is integer division, the result is 0: it's better to use 1.0/3.
Also your approximation is incorrect. The correct approximation is
n^n * e^(-n) * sqrt((2*n+1/3)*PI)
It seems you fell into the integer division trap with 1/3, which has the value 0. You need to write this with floating point constants as 1.0 / 3.0.
You probably need to type 1.0/3.0 to get one third.