View previous topic :: View next topic |
Author |
Message |
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
Integer rounding??? |
Posted: Wed Feb 03, 2016 11:31 am |
|
|
Hi All,
I have a signed 'scaled integer' that represent a Fahrenheit temperature. The integer value varies from -2500 (-25F) to +12500 (125F), with all values scaled by 100. I'd like to round these values to the nearest 10, so that my temperatures will have a single digit after the decimal, with the trailing 0 not displayed.
So, a value of 7512 would round down to 7510, and be displayed as '75.1' , and a value of 7518 would round up to 7520, and be displayed as '75.2'.
I can imagine some brute force methods to do this, but nothing simple comes to mind.
I've got the display part covered, I'm just looking for some ideas on integer rounding.
Jack |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Wed Feb 03, 2016 11:49 am |
|
|
Just use +5, then divide by 10.
7512+5 = 7517
Integer /10
7517/10 = 751
7518+5 = 7523
7523/10 = 752
etc..
You can use +4 or +5, according to where exactly you want the change to occur. |
|
![](templates/subSilver/images/spacer.gif) |
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
|
Posted: Wed Feb 03, 2016 1:46 pm |
|
|
Hi Ttelmah,
Ah! Perfect! Works like a charm!
Jack |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Thu Feb 04, 2016 1:37 am |
|
|
Well done for thinking in integer. ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Feb 04, 2016 8:13 am |
|
|
Not quite. Negative values require -5 to round off correctly
Mr T showed how to do it recently for floats.
Try something like this
(i<0? (i-5)/10: (i+5)/10 )
(I can't find a SIGN() function)
Mike |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Thu Feb 04, 2016 9:20 am |
|
|
Good point. I tend to use Kelvin, so don't have to worry about -ve values.... (Actually I might get very worried by a -ve value!...) |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Feb 04, 2016 11:54 am |
|
|
Yes, Kelvin solves a lot of problems.
Worries me when weather forecasters (the ones who should know better) talk about 20C being twice a hot as 10C.
I've yet to work out how they handle negative values.
I suppose negative Kelvin is in the group as travelling faster than the speed of light and black holes.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Thu Feb 04, 2016 12:34 pm |
|
|
Mike, all things are relative and you can go faster than the speed of light. consider that the math that says you can't is the SAME math that says bumblebees cannot fly.
I like *F over *C but the Feds here in Canada say everything has to be Metric...though when a bad guy is wanted he's ALWAYS 6' 2", 180 pounds.....
BTW this...
(i<0? (i-5)/10: (i+5)/10 )
gave me a headache trying to figure it out !!
though I think it says..
IF i < 0 THEN (i-5)/10 ELSE (i+5)/10
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Feb 04, 2016 1:44 pm |
|
|
Spot on Jay.
It's standard 'C' short hand for the ternary operator if/then/else.
I've got it in my copy of K&R and other C programming books.
A lot of the other 'C' constructs give me a headache also.
I know what I want in (say) QBasic, but have to look it up in 'C'.
(Age related?)
I tried this variation
(i + (5 * i/abs(i) ))/ 10
It works for all values and didn't fall over when i==0.
I found that surprising.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Thu Feb 04, 2016 2:14 pm |
|
|
yup, it's an age thing ! I used QB45 for a LOT of code in the 'early days', got into a mess, wrote THE Mr. Gates a one page letter,got a call few days later and 2 big packets of information about QB45. Once Windows took over the real serial ports had to rethink direction of the company,got into PICs.
Even they were a LOT simpler then ! Now they have more fuses than instructions. Isppose if you 'live and breath' C everyday you get good at it, me , I just 'bodge' it enough to get a PIC to do what I need.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Thu Feb 04, 2016 3:44 pm |
|
|
The point about ?:, is that the statement can be used in maths, to actually 'return' the different values, where the 'if' can't directly do this.
You have to be very careful about the 'faster than light' thing. There is nothing at all in the current theories, that prevent something actually travelling faster than light. What there is, is an impossibility actually _at_ the speed of light. The effective mass of the object becomes infinite, so it can't accelerate 'through' this speed. It's the inverse of an asymptote.
The logic test is a lot quicker than using an extra multiplication and division. |
|
![](templates/subSilver/images/spacer.gif) |
|