Why is this simple piece of code not working? - c

I am trying to get a floating variable accurate to just 3 decimal points for a comparison calculation. I am trying the method below, but it doesn't work. I can't see why not, so please can someone tell me where I am going wrong.
Output from this code is b = 10000.050617, bb = 10000050 and fbb = 10000.000. I want fbb to be 10000.050.
int bb; double m,n,p,q,b,t,u,fbb;
m=24.161, n=57.695, p=67.092, q=148.011;
t=p-m; u=q-n;
b=t*t+u*u; bb=b*1000; fbb=bb/1000;
printf("b=%.6lf,bb=%i,fbb=%.3lf\n",b,bb,fbb);
return 0;

When you perform
fbb = bb/1000;
It treats operation as int/int and returns an int
Try
fbb = ((double)bb)/1000.000;
It will be treated as (double)/(double) and return a double.

bb is an int, so bb / 1000 will follow the integer division. Change either or both operand to a double. The simplest way is:
fbb = bb / 1000.0; //type of 1000.0 is double
or
fbb = (double)bb / 1000

When you perform
fbb = bb/1000;
It treats operation as int/int and returns an int. its demotion of value.
Also take long bb; instead of int as int has value 32767 as its high value.
Try
fbb = bb/1000.000;
or
fbb = (double)bb/1000;

bb is int. So bb/1000 is doing a int division, which results again in an int = 1000 (no decimals). That int value is cast to a double.

Use
fbb=(double)bb/1000;
bb is integer and result is integer, and then converted to double

Related

Two different answers for same expression in C

I have an expression which does the same calculation. When I try to do the whole calculation in a single expression and store it in variable "a", the expression calculates the answer as 0. When I divide the equations in two different parts and then calculate it, it gives the answer -0.332087. Obviously, -0.332087 is the correct answer. Can anybody explain why is this program misbehaving like this?
#include<stdio.h>
void main(){
double a, b, c;
int n=0, sumY=0, sumX=0, sumX2=0, sumY2=0, sumXY=0;
n = 85;
sumX = 4276;
sumY = 15907;
sumX2 = 288130;
sumY2 = 3379721;
sumXY = 775966;
a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
b = ((n*sumXY) - (sumX*sumY));
c = ((n*sumX2) - (sumX*sumX));
printf("%lf\n", a);
printf("%lf\n", b/c);
}
Output:
0.000000
-0.332097
In your program
a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
all the variables in the right hand side are of type int, so it will produce a result of type int. The true answer -0.332097 is not a int value, so it will be converted to a valid int value, namely 0. And this 0 is assigned to variable a.
But when you do
b = ((n*sumXY) - (sumX*sumY));
c = ((n*sumX2) - (sumX*sumX));
printf("%lf\n", b/c);
The variable b and c are of type double, so the expression b/c produce a double typed value and the true answer -0.332097 is a valid double value. Thus this part of your code give a right result.
In first equation i.e. a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX)) both numerator and denominator will give integer results and the value stored in a will also be integer as integer/integer is integer. In second and third expression as you are solving them individually both b and c will be stored in double and double/double will result in a double i.e. a decimal value.
This problem can be solved by using type casting - or better still using float for the variables.
Add double before your calculation, so after you do your integer calculation in "a", it will convert it to double.
a = (double)((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
First of all (INTEGER)/(INTEGER) is always an INTEGER. So, you can typecast it like a = (double)(((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX)));
OR
We know that any number (n ∈ ℂ), multiplied by 1.0 always gives the same number (n). So, your code shall be like:
a = ((n*sumXY*1.0L) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
Multiplying by 1.0L or 1.0f converts the whole arithmetic operation to long double data type.
Now you can print the number (-0.332097) to stdout.
Non-standard Code
Your code is:
void main()
{
//// YOUR CODE
}
Which is non-standard C. Instead your code should be like:
int main(int argc, char **argv)
{
//// YOUR CODE
return 0;
}
Change all your INTEGERS to DOUBLES. That should solve the problem
The type of 1st expression is int whereas in 2nd expression it is double.

Am I dividing in the right way in C?

I need to apply a formula to three variables. Some of the operations (divisions) give me a 0 instead of the proper number. So I get a different result from what I expected from the formula.
I think that the problem is related to the way C manages data types and their remainders. For this reason, I tried to transform int variables into float variables or to round the numbers that I divided. But all of this is still not working and I'm not able to understand what I'm missing.
Could you please have a look at my code and let me know where is the mistake?
Thanks
double grade(int lc, int wc, int sc)
{
wc = (float)wc;
lc = (float)lc;
sc = (float)sc;
float L = round((wc/100)/lc);
float S = round((wc/100)/sc);
float index = 0.0588 * L - 0.296 * S - 15.8;
return index;
}
Problem:
wc = (float)wc;
lc = (float)lc;
sc = (float)sc;
All these statements do not do what you think because the types of these variables are originally int.
You are converting an int to float and assigning it back to int.
Solution:
Pass the values to a float rather than int to the function, implicit conversion will take care of the rest.
Change your function signature to
double grade(float lc, float wc, float sc)
Or just do this (the other operands will not need the cast to float since it enough if only one of the operands is it, then the others will be converted to it implicitly)
double grade(int lc, int wc, int sc)
{
float L = round(((float)wc/100)/lc);
float S = round(((float)wc/100)/sc);
float index = 0.0588 * L - 0.296 * S - 15.8;
return index;
}
Note that the return type is double, but you use just floats. Use double instead of floats in the function as types as well OR change return type to float.
The mistake is that this wc = (float)wc; is essentially this wc = (int)(float)wc; since wc type is int. Basically nothing happens, therefore when you make your calculation like wc/100, then this will be an integer division resulting in integer, so if wc is less than 100 (and >= 0), then the result is 0 and not a real number as you would expect.
wc = (float)wc; casts an int to a float, converts back to int, then assigns. No real benefit here.
wc/100 is integer division, which discards the fraction of the quotient. Not likely what OP wants.
Converting int to float risks losing precision as so a less precise answer. double is better for wide ranging int values.
Code mixes uses of float and double with a call to a double round(double), double multiplication and conversions from float to double to float to double. Simplify and use double throughout.
double grade(int lc, int wc, int sc) {
// v------v `double` division
double L = round(wc/100.0/lc);
double S = round(wc/100.0/sc);
return 0.0588 * L - 0.296 * S - 15.8;
}

Take two numbers to one number

I have two int values that I want to combine into a decimal number. So for example, I have A = 1234 and B = 323444. Both are int and I do not want to change it if possible.
I want to combine them to get 1234234233.323444.
My initial method was to divide b by 1e6 and add it to A to get my value.
I assigned
int A = 1234234233;
int B = 323444;
double C;
A = 1234;
B = 323444;
C = A + (B/ 1000000);
printf("%.6f\n", C);
I get 1234234233.000000 as a result. It rounds my C and I do not want that as I want 1234234233.323444
how can I solve this?
Try like this:
C = A + (B/ 1000000.0);
ie, make the denominator as double so that when integer by integer division is made it does not return weird results like you are getting.
NOTE:-
Integer/Integer = Integer
Integer/Double = Double
Double/Integer = Double
Double/Double = Double
B is an integer and dividing an integer by another integer (10000 here) will always give an integer and that's why you are getting unexpected result. Changing 10000, which is of type int, to 10000.0 (double type) will solve this problem. It seem that 10000 and 10000.0 are integer by mathematical definition but both are of different type in programming languages, former is of type int while latter is of type double.
C = A + (B/ 1000000.0);
or
C = A + ((double)B/ 1000000);
to get the expected result.

Angelscript wrong type conversion

I'm currently trying to use angelscript with a simple code, following the official website's examples.
But when i try to initialise a double variable like below in my script :
double x=1/2;
the variable x appears to be initialised with the value 0.
It only works when i write
double x=1/2.0; or double x=1.0/2;
Does it exist a way to make angelscript work in double precision when i type double x=1/2 without adding any more code in the script ?
Thank you,
Using some macro chicanery:
#include <stdio.h>
#define DIV * 1.0 /
int main(void)
{
double x = 1 DIV 2;
printf("%f\n", x);
return 0;
}
DIV can also be defined as:
#define DIV / (double)
When you divide an int by an int, the result is an int. The quotient is the result, the remainder is discarded. Here, 1 / 2 yields a quotient of zero and a remainder of 1. If you need a double, try 1.0 / 2.
No, there is no way to get a double by dividing two ints without casting the result.
That's because 1 and 2 are integers, and:
int x = 1/2;
Would be 0. If x is actually a double, you get an implicit cast conversion:
double x = (double)(1/2);
Which means 1/2 = 0 becomes 0.0. Notice that is not the same as:
double x = (double)1/2;
Which will do what you want.
Numbers with decimals are doubles, and dividing an int by a double produces a double. You can also do this with casting each number:
double x = (double)1/(double)2;
Which is handy if 1 and 2 are actually int variables -- by casting this way, their value will be converted to a double before the division, so the product will be a double.

Dividing integers in C rounds the value down / gives zero as a result

I'm trying to do some arithmetic on integers. The problem is when I'm trying to do division to get a double as a result, the result is always 0.00000000000000000000, even though this is obviously not true for something like ((7 * 207) / 6790). I have tried type-casting the formulas, but I still get the same result.
What am I doing wrong and how can I fix it?
int o12 = 7, o21 = 207, numTokens = 6790;
double e11 = ((o12 * o21) / numTokens);
printf(".%20lf", e11); // prints 0.00000000000000000000
Regardless of the actual values, the following holds:
int / int = int
The output will not be cast to a non-int type automatically.
So the output will be floored to an int when doing division.
What you want to do is force any of these to happen:
double / int = double
float / int = float
int / double = double
int / float = float
The above involves an automatic widening conversion - note that only one needs to be a floating point value.
You can do this by either:
Putting a (double) or (float) before one of your values to cast it to the corresponding type or
Changing one or more of the variables to double or float
Note that a cast like (double)(int / int) will not work, as this first does the integer division (which returns an int, and thus floors the value) and only then casts the result to double (this will be the same as simply trying to assign it to a double without any casting, as you've done).
It is certainly true for an expression such as ((7 * 207) / 6790) that the result is 0, or 0.0 if you think in double.
The expression only has integers, so it will be computed as an integer multiplication followed by an integer division.
You need to cast to a floating-point type to change that, e.g. ((7 * 207) / 6790.0).
Many poeple seem to expect the right-hand side of an assignment to be automatically "adjusted" by the type of the target variable: this is not how it works. The result is converted, but that doesn't affect any "inner" operations in the right-hand expression. In your code:
e11 = ((o12 * o21) / numTokens);
All of o12, o21 and numTokens are integer, so that expression is evaluated as integer, then converted to floating-point since e11 is double.
This like doing
const double a_quarter = 1 / 4;
this is just a simpler case of the same problem: the expression is evaluated first, then the result (the integer 0) is converted to double and stored. That's how the language works.
The fix is to cast:
e11 = ((o12 * o21) / (double) numTokens);
You must cast these numbers to double before division. When you perform division on int the result is also an integer rounded towards zero, e.g. 1 / 2 == 0, but 1.0 / 2.0 == 0.5.
If the operands are integer, C will perform integer arithmetic. That is, 1/4 == 0. However, if you force an operand to be double, then the arithmetic will take fractional parts into account. So:
int a = 1;
int b = 4;
double c = 1.0
double d = a/b; // d == 0.0
double e = c/b; // e == 0.25
double f = (double)a/b; // f == 0.25

Resources