Author Topic: ROUNDING DOWN OR UP?  (Read 3529 times)

Offline SIMON BAILIE

  • Commander
  • *****
  • Posts: 676
    • View Profile
ROUNDING DOWN OR UP?
« on: October 02, 2015, 07:03:50 pm »
Having done a couple of spreadsheets for psionic percentage chances and in my own games worked out the stated accuracy of a shot from the formula in https://ufopaedia.org, is it hardcoded or is there another reason why openxcom and probably the original games "trunciate" fractions, in other words if a stated accuracy of a shot has a 81.62% chance why is it rounded down to 81 instead of following the law of mathematics and rounding up to 82?

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
Re: ROUNDING DOWN OR UP?
« Reply #1 on: October 02, 2015, 07:33:17 pm »
I would guess that it is because many of those variables are defined as integers in the code and, in integer mathematics, 4/3 = 1, because the rest doesn't fit.

It makes for very interesting math:
10 * 134/100 = 13
10 * (134/100) = 10
100 * 134/100 = 134
10 * ( 10 * 134/10) = 130
100 * (134/100) = 100


Or even:
1000*134/1000 = 134
1000 * (134/1000) = 0


And for the follow up "why did they use integers?!", it is simply because it takes less of a computer's memory to store and track integer variables than floating point numbers, since the computer can forget about the decimals. Not an issue today, but back when XCom came on floppy discs, computers didn't have any to spare for such trivialities as decimals.

Offline Warboy1982

  • Administrator
  • Commander
  • *****
  • Posts: 2333
  • Developer
    • View Profile
Re: ROUNDING DOWN OR UP?
« Reply #2 on: October 03, 2015, 03:47:40 am »
integers also have a lot fewer inherent problems. floating point accuracy is the bane of the programmer, because the way you see a number and the way a computer sees it are very different.
for example, when you see "0.0" you think 'fair enough, it's zero' but the computer sees something more like "0.0000000000000001" this can lead to a LOT of errors in math, because the computer does absolute calculations with these abstract numbers, so 0.0 * 10000000000000000 = 1.
and it gets worse.
40.0 * 7.2 will give you a SLIGHTLY different result than 7.2 * 40.0. not enough that you or i would notice, but the computer DOES.

at the end of the day, integers rule the earth, and if you try to use floating points for everything, you're not only setting yourself up for failure, but also the worst nightmarish debugging you've ever seen.

we've wrestled with these issues a lot ourselves.

https://github.com/SupSuper/OpenXcom/commit/8d0363b0b7b9258830c9971a1b42bd1a0ae0b1ca

https://github.com/SupSuper/OpenXcom/commit/ab2f6a40dad4c8ca83267cb436fbf097543d8a32
« Last Edit: October 03, 2015, 03:58:03 am by Warboy1982 »

Offline Hobbes

  • Commander
  • *****
  • Posts: 2102
  • Infiltration subroutine in progress
    • View Profile
Re: ROUNDING DOWN OR UP?
« Reply #3 on: October 03, 2015, 03:53:31 am »
Code: [Select]
if the city's longitude and latitude doesn't match the city's longitude and latitude
notify the local authorities, as all logic and reason has just gone out the window.

"Sir... are you on drugs?"  ::)

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
Re: ROUNDING DOWN OR UP?
« Reply #4 on: October 03, 2015, 04:02:46 am »
Haha! Very true.. it is not *just* a memory issue and this kind of stuff can be a good enough reason to keep integers even in our world where memory isn't so much of an issue.

In my world of "scientific programming", which means taking a bunch of people with absolutely no clue how to program and asking them to learn by looking at code written by other people who didn't learn how to properly program either, it can be very interesting indeed...

(Note: I'm one of these people too.. We try our best, but a programmer would go blind seeing what we do for a living, which is what gives everybody the weather forecast, amongst other things).