Rounding of player credits

For test results, bug reports, announcements of new builds etc.

Moderators: winston, another_commander, Getafix

Post Reply
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Rounding of player credits

Post by Eric Walch »

I just got a mission screen with my credits listed as : 6693135.800000001

The problem is the rounding of player.credits. In oolite the introduction of a rounding error happens when converting the credits to a JS number.

My question: Is this an oolite bug and should it do additional rounding before returning a value OR is this the responsibility for the script to do all the rounding of player credits?
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Re: Rounding of player credits

Post by another_commander »

Good question. I wouldn't call it an Oolite bug, to be honest. I guess a different way of putting it would be: Is there any chance, ever, that a script might require to show the player any credit value past the first decimal on a mission screen? If yes, then we leave it as is. If no, then we could consider rounding it to one decimal in code.

In any case, it is very easy to print a number with a determined amount of decimals shown. In your case, the credits value somehow went to 6693135.800000001. For ensuring that this number is displayed on a mission screen with one decimal only, one can do as an example

Code: Select all

mission.runScreen({
	titleKey: "theMissionTitle",
	message: "credits: " + player.credits.toFixed(1)
})
Appending .toFixed(howManyDecimals) to any number should be enough to display it as one wants.
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Rounding of player credits

Post by Eric Walch »

another_commander wrote:
Good question. I wouldn't call it an Oolite bug, to be honest.
That depends how you look at it. Internally the player credits is maintained at a precision of one digits. (Actually as integer in deci-crecredits). So from the internal code it feels somehow as a bug that it can return a slightly different value.

I just thought it would be more convenient for scripts if the code would make sure no rounding errors would show up.
But the code player.credits.toFixed(1) is just as easy, as long as programmers are aware of the need of avoiding rounding bugs. For some reason I did not knew that JS command and always used Math.round(player.credits*10)/10
And a big advantage of player.credits.toFixed(1) is that it will also create numbers with one decimal like "34.0". So for showing the player.credits it even feels like a must to use.
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Rounding of player credits

Post by Switeck »

This rounding issue is a bigger problem than just credits.

I've encountered similar problems with galaxy map X, Y (0-255) native coordinates returning long decimal numbers when they should be integers for systems.
And converting them to Light Year X,Y coordinates via:
X-LY = 0.4*Math.floor(X);
Y-LY = 0.2*Math.floor(Y);
...just seems to make the problem worse. :(
User avatar
CommonSenseOTB
---- E L I T E ----
---- E L I T E ----
Posts: 1397
Joined: Wed May 04, 2011 10:42 am
Location: Saskatchewan, Canada

Re: Rounding of player credits

Post by CommonSenseOTB »

Would "player.credits.toFixed(1)" work for other variables like this.myownvariable.toFixed(1) for use for example to keep custom hud gauge variables rounded to 1 decimal place, for example when dealing with amount of fuel the player has and displaying it properly?
Take an idea from one person and twist or modify it in a different way as a return suggestion so another person can see a part of it that can apply to the oxp they are working on.


CommonSense 'Outside-the-Box' Design Studios Ltd.
WIKI+OXPs
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Re: Rounding of player credits

Post by Kaks »

.toFixed() is a js method, so it can be applied to any number. See:

http://www.w3schools.com/jsref/jsref_tofixed.asp
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
User avatar
CommonSenseOTB
---- E L I T E ----
---- E L I T E ----
Posts: 1397
Joined: Wed May 04, 2011 10:42 am
Location: Saskatchewan, Canada

Re: Rounding of player credits

Post by CommonSenseOTB »

Kaks wrote:
.toFixed() is a js method, so it can be applied to any number. See:

http://www.w3schools.com/jsref/jsref_tofixed.asp
Thank you Kaks, that great! :)
Take an idea from one person and twist or modify it in a different way as a return suggestion so another person can see a part of it that can apply to the oxp they are working on.


CommonSense 'Outside-the-Box' Design Studios Ltd.
WIKI+OXPs
Post Reply