C programming about "double" multiplication - c

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

Related

7%9 is showing different answers when using integer and float format specifiers. Why is it so?

# include<stdio.h>
int main() {
printf("%d \n", 7%9); //integer result
printf("%f", 7%9); //float result
return 0;
}
Above is the code I used to calculate the value of 7%9. I wanted to see both the integral and the float result. But, the values appearing here are different.
printf("%d \n", 7%9); //integer result
printf("%f", 7%9); //float result
Although the comments and the format specifiers indicate your intent, the compiler sees the integer calculation and passes an integer value to printf().
In the 2nd line, you've effectively written:
printf( "%f", 7 );
That is obviously incorrect.
If you wanted to see lots of zeros, you could cast the value to match what you've written as the format specifier:
printf( "%f", (float)( 7 % 9 ) );

How to convert int to Double in C programming

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

How to divide 2 int in c?

wanna divide 2 numbers and get the result like this:
5 / 2 = 2.50
But it only outputs 2.
I don't now what i'm doing wrong.
Here my code:
int a;
int b;
int c;
printf("First num\n");
scanf("%d", &a);
printf("Second num\n");
scanf("%d", &b);
c = a / b;
printf("%d", c);
You need a double variable to store the result. int stores only integers. Additionally, you have to typecast the other variables also before performing the division.
Do something like this
double c;
.
.
.
c = (double)a / (double)b;
printf("%f", c);
NOTE:
You do not need the & in printf() statements.
To avoid the typecast in float you can directly use scanf with %f flag.
float a;
float b;
float c;
printf("First number\n");
scanf("%f", &a);
printf("Second number\n");
scanf("%f", &b);
c = a / b;
printf("%f", c);
The '/' - sign is for division. Whenever in C language, you divide an integer with an integer and store the data in an integer, the answer as output is an integer. For example
int a = 3, b = 2, c = 0;
c = a/b; // That is c = 3/2;
printf("%d", c);
The output received is: 1
The reason is the type of variable you have used, i.e. integer (int)
Whenever an integer is used for storing the output, the result will be stored as integer and not a decimal value.
For storing the decimal results, C language provide float, double, long float and long double.
Whenever you perform an operation and desires an output in decimal, then you can use the above mentioned datatypes for your resultant storage variable. For example
int a = 3, b = 2;
float c = 0.0;
c = (float)a/b; // That is c = 3/2;
printf("%.1f", c);
The output received: 1.5
So, I think this will help you to understand the concept.
Remember: When you are using float then the access specifier is %f. You need to convert your answer into float, just as I did, and then the answer will be reflected.
You have to use float or double variables, not int (integer) ones. Also note that a division between two integers will lead to an integer result, meanwhile a division between a float/double and an integer will lead to a float result. That's because C implicitly promote this integer to float.
For example:
5/2 = 2
5/2.0f = 2.5f
Note the .0f, this actually means that we are dividing with a float.
In C, only an int type number is displayed. 5/2 gives a floating point type number. So, the compiler compiles it only with the integer value.

Convert a Based-n Number To Decimal But Doesn't Work

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.

C math not respecting declared constants

Stackoverflow,
I'm trying to write a (very) simple program that will be used to show how machine precision and flops effect functions around their root. My code is as follows:
#include <stdio.h>
#include <math.h>
int main(){
const float x = 2.2;
float sum = 0.0;
sum = pow(x,9) - 18*pow(x,8) + 144*pow(x,7) - 672*pow(x,6) + 2016*pow(x,5) -
4032*pow(x,4) + 5376*pow(x,3) - 4608*pow(x,2) + 2304*x - 512;
printf("sum = %d", sum);
printf("\n----------\n");
printf("x = %d", x);
return 0;
}
But I keep getting that sum is equal to zero. At first I thought that maybe my machine wasn't respecting the level of percision, but after printing x I discovered that the value of x is changing each time I run the program and is always huge (abs(x) > 1e6)
I have it declared as a constant so I'm even more confused as to whats going on...
FYI I'm compiling with gcc -lm
printf("sum = %d", sum);
sum is a float, not an int. You should use %f instead of %d. Same here:
printf("x = %d", x);
Reading about printf() format specifiers may be a good idea.

Resources