Subtraction of fractions giving the wrong answer.

Hi, I'm making a program for my Chemistry course at uni that does a certain calculation for me. However I've noticed it's returning the wrong answer. I took a closer look at the part which is doing the calculation, highlighted in bold. And as far as I'm aware the problem seems to be arising in the step where T22 is subtracted by T11. It keeps giving the value as 0, which is wrong. Does anyone know why it could be doing this because I don't have any idea.

Thanks Alex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/A program that uses the arrhenius equation to calculate and print A and E_a to the screen.
//The following libraries will be used.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

double Ea(int T1, int T2, double K1, double K2, double R)//Works out the value of Ea.
{
    double Ea, T11, T22, T, K;
    T11=(1/T1);
    T22=(1/T2);
    T=T11-T22;
    printf("%lg = T\n",T);

    K=log(K1)-log(K2);
    printf("%lg = K\n",K);

    printf("%lg = R\n",R);

    Ea=-((R*(log(K1)-log(K2)))/((1/T1)-(1/T2)));
    printf("\n%lg",Ea);
    return (Ea);

}

int main()//The Main Function. Prints Introductory Messages and asks for the data which will be used in the functions.
{
    int T1,T2;//Defines the integer variables that need to be inputted.
    double K1,K2;//Defines the double variables that need to be inputted.
    double R;//Definites the double variabe the boltzmann constant.

    R=8.314;//Defines the value of R.

    printf("A Program to work out A and Ea using the Arrhenius equation.\n");//Introductory Message.

    printf("\nPlease enter the values of T1(Temperature 1) and its corresponding\nreaction rate constant k1.\n");//Asks for the user to input T1 and k1.
    printf("T1=");
    scanf("%d",&T1);//Obtains the value of T1
    printf("k1=");
    scanf("%lg",&K1);//Obtains the value of k1


    printf("\nPlease enter the values of T2(Temperature 2) and its corresponding\nreaction rate constant k2.\n");//Asks for the user to input T2 and k2.
    printf("T2=");
    scanf("%d",&T2);//Obtains the value of T2
    printf("k2=");
    scanf("%lg",&K2);//Obtains the value of k2

    printf("T1 is %d and K1 is %lg, T2 is %d and K2 is %lg\n",T1,K1,T2,K2);//Confirmation Message to check the inputted values.

    Ea(T1,T2,K1,K2,R);//Runs the function Ea which calculates the value of Ea using the data from the int main() function.

    return 0;
}
closed account (o3hC5Di1)
Hi there,

I believe the problem is that you're dividing two integers, which "naturally" yields an int result. so any pointing float value between zero and 1 is likely to be rounded off to 0.

The solution to this is to cast one of the two operators to a floating point value, the second int will then implicitly be casted to the same type and that will give a floating point value:

10
11
12
13
double Ea, T11, T22, T, K;
T11=(1.0F/T1);

//or: T11=(1/static_cast<double>(T1)); 


Or just pass T1 in as a type double, instead of int.

Hope that helps.

All the best,
NwN
Last edited on
T1 and T2 are integers.
Hence 1/T1 and 1/T2 will perform integer division.

If you put 1.0/T1 instead, then 1.0 will be of type double and a floating-point divide will be done.
1
2
3
4
5
6
double Ea(int T1, int T2, double K1, double K2, double R)
{
    double T11, T22;
    T11=(1/T1);
    T22=(1/T2);
    ...


Yey,you fixed it for me.
Thank you VERY! much for your help.
Much appreciated.
@NwN T11=(1.0F/T1); Using a float here may lose precision.

1
2
3
4
5
6
7
    double A = 1/3;
    double B = 1.0f/3;
    double C = 1.0/3;

    printf("A: %16.15f\n", A);
    printf("B: %16.15f\n", B);
    printf("C: %16.15f\n", C);

Output:
A: 0.000000000000000
B: 0.333333343267441
C: 0.333333333333333

Last edited on
Ohhh thankyou very much for that Chervil!
A correction - I just realised that the code sample which I posted, though useful to illustrate the ideas, may be misleading. That is, I use compile-time constants which may behave a little differently to a run-time calculation.
1
2
3
4
5
6
7
8
    int T1 = 3;
    double A = 1/T1;
    double B = 1.0f/T1;
    double C = 1.0/T1;

    printf("A: %17.16f\n", A);
    printf("B: %17.16f\n", B);
    printf("C: %17.16f\n", C);

Output:
1
2
3
A: 0.0000000000000000
B: 0.3333333333333333
C: 0.3333333333333333

At least on my system, the value of B seems to behave as double rather than float in this case.
closed account (o3hC5Di1)
You're right Chervil, from this sites documentation:

The default type for floating point literals is double. If you explicitly want to express a float or a long double numerical literal, you can use the f or l suffixes respectively


So not using 'F' would be recommended here, rather just use "1.0" and that should give you a double.

My apologies for any confusion.

All the best,
NwN
Topic archived. No new replies allowed.