#I guess there's something wrong with the k_to_f() function. Please help

15 messages · Page 1 of 1 (latest)

pine flare
#
#define c_to_k(x) x+273.15
#define k_to_c(x) x-273.15
#define c_to_f(x) (1.8)*x+32
#define f_to_c(x) (x-32)*(5.0/9)
#define f_to_k(x) c_to_k(f_to_c(x))
#define k_to_f(x) c_to_f(k_to_c(x))
void convert_temp()
{
    int unit1=0,unit2=0;
    long double val;
    enum {zero,k,f,c};
    
    line;
    printf("\n[1] Kelvin(K)\n[2] Fahrenheit(°F) \n[3] Celsius(°C)\n");
    line;
    
    printf("\nConvert from : ");
    scanf("%d",&unit1);
    printf("\nConvert to : ");
    scanf("%d",&unit2);
    line;
    
    if(unit1==k)
    {
        printf("\nEnter the temperature in K : ");
        scanf("%Lf",&val);
        if(unit2==k)
            printf("\nTemperature in K : %Lf",val);
        else if(unit2==f)
            printf("\nTemperature in °F : %Lf",k_to_f(val));
        else if(unit2==c)
            printf("\nTemperature in °C : %Lf",k_to_c(val));
    }
    else if(unit1==f)
    {
        printf("\nEnter the temperature in °F : ");
        scanf("%Lf",&val);
        if(unit2==k)
            printf("\nTemperature in K : %Lf",f_to_k(val));
        else if(unit2==f)
            printf("\nTemperature in °F : %Lf",val);
        else if(unit2==c)
            printf("\nTemperature in °C : %Lf",f_to_c(val));
    }
    else if(unit1==c)
    {
        printf("\nEnter the temperature in °C : ");
        scanf("%Lf",&val);
        if(unit2==k)
            printf("\nTemperature in K : %Lf",c_to_k(val));
        else if(unit2==f)
            printf("\nTemperature in °F : %Lf",c_to_f(val));
        else if(unit2==c)
            printf("\nTemperature in °C : %Lf",val);
    }
}

I tested using the converter in my phone. The rest give same results. Only from Kelvin to Fahrenheit, I don't get the values correct

frank patioBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

storm anchor
#

Without really looking at the code besides the definitions my first thought is that it's an error because of the macros. Mainly because of this:

#define SUM(x, y) x + y

int main() {
    int x = 3 * SUM(4, 2);
}

Now you might expect this to evaluate to 3 * (4 + 2) but remember this is a 1-1 text replacement so what actually happens is:

#

;asm -E

#define SUM(x, y) x + y

int main() {
    int x = 3 * SUM(4, 2);
}
radiant deltaBOT
#
Assembly Output
# 0 "/app/example.c"
# 1 "/app//"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "/app/example.c"

int main() {
    int x = 3 * 4 + 2;
}

storm anchor
#

So this gets evaluated as (3 * 4) + 2 = 14 which is a totally different value than 3 * (4 + 2) = 18.

#

What you should do is wrap everything that might be ambiguous in parentheses or simply define them as functions (possibly with inline to indicate you want it inlined for the same performance as macros).

#

F.e. for the sum macro this fixes it:

#define SUM(x, y) (x + y)

... Except it doesn't!
Look at this:

Update: I've actually been unable to break the sum example, so here's a new macro:

#

;asm -E

#define MUL(x, y) (x * y)

int main() {
    int x = MUL(4, 1 + 2);
}
radiant deltaBOT
#
Assembly Output
# 0 "/app/example.c"
# 1 "/app//"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 0 "<command-line>" 2
# 1 "/app/example.c"

int main() {
    int x = (4 * 1 + 2);
}

storm anchor
#

We'd expect this to evaluate to 4 * (1 + 2), but it evaluates to (4 * 1) + 2`.
So you also need to wrap the parameters:

#define MUL(x, y) ((x) * (y))
pine flare
#

Oh. Now I understand

#

Let me try

#

Thanks @storm anchor. It woked

#

!solved