Tom Ramcigam (magicmarmot) wrote,
Tom Ramcigam

One more brain teaser for the day. This one's a bit more complex, and has to do with coding.

You are managing the design of a temperature conversion for a temperature controller in software. One of the problems is that you get the temperature as a floating-point number (i.e 19.9892653), but the temperature controller only accepts a 16-bit integer that is 10 times the actual temperature (i.e. for 20.4 degrees, it wants 204).

The first time you try this with a simple cast:

int MyTemp = (int)(YourTemp * 10);

unfortunately you discover that this simply truncates the number, so if YourTemp comes in as 19.9892653,
MyTemp comes out as 199 instead of 200.

Perusing your C programming book, you realize that the math library has no proper rounding function, so you have to create one.

How do you do it?

Bear in mind that you also have to deal with temperatures below zero.
Tags: brainy
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.