Hacker News new | past | comments | ask | show | jobs | submit login

Also, rearrange the calculation. (Celsius * 9) / 5 can trivially be rearranged to Celsius * (9 / 5)

You can implement this as a fixed point multiply by the constant 1.8.

You are already using base 10 fixed point, so it simply becomes a multiply by 18, which can be simplified to a few shifts and adds:

(Celsius << 4) + Celsius + Celsius

Don't forget to add 32 and the algorithm requires a 16bit shift and three 16 bit adds.




>Also, rearrange the calculation.

Yep, for the nand2tetris course, we had division, but it was in the OS, and a really expensive operation. I wrote a program that needed to know which third of a line a point was on. (i.e. identify 4 on a 15-length line as being in the first third).

I naively wanted to do something like "if x < L / 3", but rearranged it to "if x * 3 < L". (Of course, the language only supported integers and not fractions or floating point, so I needed to do that anyway.)


But then you are in base 100 fixed point. Converting it back to base 10 is what requires a division (but it's still by a constant though).


Op is already in base 100 fixed point. I guess because that's what the sensor returns?

Though it might be smart to convert it to another format. Four nibbles of BCD would be ideal and the MCS-48 looks to have native support for it.

Thought you would need a custom version of shift by 4 that handles BCD.

Or simplify the equation to just adds:

    C2 = C + C
    C4 = C2 + C2
    C8 = C4 + C4
    C16 = C8 + C8
    Res = C16 + C2


If he's in base 100 and you multiply by 18 it's the same story: you are now in base 1000 and need a division to go back.

Converting from binary to BCD also needs division by constants, so again it's more or less the same.


Oh, right. I'm an idiot who forgot about the fixed point divide.

And yes, at some point you are going to need to do a divide by constant 10. It's unavoidable if you want a base 10 display. The option of using BCD to do calculations just lets you do that divide upfront, rather than at the end.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: