[SOLVED] Need another pair of eyes for elementary C program

Just trying to convert Farenheit to Celcius. They book gave me the conversion formula. For one reason or another the output for celcius is always 0.00000 as shown below.

got an itch

still got an itch

I’m pretty sure I messed up the variable assignment cel in the middle but for the life of me I can’t figure out what I goofed on. Thanks in advance for any help you can give me.

1 Like

100 / 180 equals zero, because you are dividing integers. Your program is effectively multiplying by zero.

Either

  • divide by 180.0 instead 180
  • first multiply by 100, then divide by 180
5 Likes

180.0 is a double value. 180.0f is a float. Rewrite line 11 as

cel = 100 / 180.0f * (far - 32);

and you have floats all the way down.

Yep. That’s it. Worked just fine.

So this is a consequence of my using float variables? I tried to do it with int variables and i got a similar, non sense result.

1 Like

You are correct of course, but that hardly matters in this program.

1 Like

The problem is that in C dividing two integers yields another integer. 1 / 2 would result in 0.5, but that is not an integer value. So C truncates the result to 0.

The same happens to the 100 / 180 in your program. The result cannot be represented exactly and is truncated to 0. Thus (100 / 180) * (far - 32) becomes 0 * (far - 32), which equals zero.

3 Likes

To be more precise than what @pFtpr said, you may want to use explicit number constants, so type 180f instead of just 180

When you write a floating-point constant in a program, in which floating-point type does the program store it? By default, floating-point constants such as 8.24 and 2.4E8 are type double. If you want a constant to be type float, you use an f or F suffix. For type long double, you use an l or L suffix. (Because the lowercase l looks a lot like the digit 1 , the uppercase L is a better choice.)

Reading up on type casting and also the shortcomings on the float type is useful, even if it isn’t required in this snippet.

Type casting: https://www.cprogramming.com/tutorial/lesson11.html

4 Likes

I’m glad explicit type casting was brought up. I didn’t realize you could type cast by adding f to the end of the number. I thought the type could be an issue so I tried float 100/ float 180. I see that’s silly now but I had that idea…my book doesn’t go that in depth so I was guess at the time lol

EDIT: lol nevermind. I’ll take this step by step. Thank you all for your help and insight. The take away for me:

1 Like

Type casting can work in the way that you mentioned but you have to do it as follows

(float) 100  / 180 
(float) 100 / (float) 180
((float) 100) /180
100 / ((float) 180)
(float) (100 / 180)
or
((float) 100 / 180)

It all depends on how through you want to be and your coding style. The standard way in the C world when dealing with constants as mentioned by the others.

I would go one step further to define your constants globally at the top of your program so that you only have to change that one parameter. Remember, never hard code in the program what you can represent with a constant variable. If the book example told you to do it that way, shame shame shame.

4 Likes

When I said the book gave me the formula this is what they supplied.

Makes sense.

I better understand now. I guess it has not touched on global constants, style, or any of the stuff that allow you to be more flexible yet.

Good luck on your adventure.

2 Likes

Actually, they touched on it a bit. Are you suggesting that (100/180) part be a global constant like this outside of the main?

#define thisIsAConstant = 100.0/180.0

worked just fine this way.

I believe the pertinence of efficient code should be scattered throughout, in every relevant bit of every lesson. Here, it would have made much more sense for the book to simply say something along the lines of

“Write a program that changes a temperature …” using the formula for conversion, instead represented as constants where possible, and reduced:

Celsius = ( 5 / 9 ) * ( Fahrenheit - 32 )

It’s not really the best for the formula to be represented like how it is in your book. If anyone knows whether the gcc reduces these things during compilation, I’d love to hear from you.

It does. This is one of the most trivial optimizations out there, so every self respecting compiler will do this for you. Never write uglier, unreadable code when the compiler will fix it anyway.

The relevant optimizations are called constant folding and constant propagation in case you want to look into them.


OPINION: Moving the values into global variables or doing algebraic optimizations makes your code harder to read, because it forces the reader to look at different places or “unoptimize” calculations to understand whats going on.

The formula for converting between Fahrenheit and Celsius is fixed. It’ll never, ever change. So parametrizing it makes no sense to me. So I’d write (far - 32) * 100 / 180 and be done with it.

1 Like

@fruitbrut
Yeah that is what I was saying. @pFtpr brings up a good point here.

He pointed out that that is his opinion. This is one of those style things and creative control things. Depending on the camp that you are from, you will agree, disagree, or feel indifferent. In this particular case I agree with him but I think that this would have been a good opportunity to have you used a global constant.

I tend to make helper programs and helper classes that I reuse a lot. As such, sometimes the constants change depending on what I am doing. If I needed to make a generic calculation program, I would personally pull my constants out and put them into global constant so that I can quickly change the program to suit my needs. You will find with programming and development in general that the majority of your work tends to be copying and pasting previous work that you have done. I am a firm believer in the Unix philosophy of write simple programs that do one thing very well (perform one type of action). You can then string them together to do more complex things.

Is that the K&R book?