another (different) rounding problem

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Re: another (different) rounding problem

Postby lwjwillemsen » Thu Sep 21, 2017 2:00 pm

Mathematical rounding should always be correct in my opinion


That's the book all about:

A computer can not do true mathematical rounding due to the representation of numbers in a series of bits / bytes.
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 580
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

Re: another (different) rounding problem

Postby jcompagner » Thu Sep 21, 2017 2:46 pm

lwjwillemsen wrote:
Mathematical rounding should always be correct in my opinion


That's the book all about:

A computer can not do true mathematical rounding due to the representation of numbers in a series of bits / bytes.


exactly

i was already really confused about that statement..
Because there is no such thing a "Mathematical rounding" if you work with floating points notation numbers!!
you shouldn't really round
you only round because of " financial administration challenges."

if you calculate stuff like summing/dividing/multiply you name it, you should never round there, only the end result if " financial administration" requires that.

This should be really clear to everybody here, that rounding in javascript is NOT possible, you never are guaranteed that you get exactly that number (with 2 decimals)

looking at harjo's number:
4.725

if you round that in the few ways that are represented here then you get
4.72 righ? that is because it is very likely 4.724999999 (because 4.725 can be expressed exactly like that) where you start with or that 4.73 can't be exactly be shown like that and you really get 4.729999999999
thats why that +0.000001 works
but still you dont really have 4.73 ... what you really have is 4.73000001

That you then store that in a database of NUMERIC(10,2) that will delete the 00001 from it, but when you read it back it it can be that Servoy really reads it as BigDecimals (a real Numeric represenation class)
but at the moment you touch it in js it will be a floating point again and it will have something after it..
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8029
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: another (different) rounding problem

Postby omar » Thu Sep 21, 2017 3:19 pm

I decided to do some rigorous tests on the different rounding methods above to get to the bottom of this and literally found thousands of rounding errors.

For example Marcs routine fails when rounding 1/201 (equals 0.004975124378109453) to 2 decimals returning 0.01 instead of 0.00. Another: 0.146 with precision 1 rounds to 0.2 etc.

Roberts routine fails when rounding 1/6667 (equals 0.000149992500037498125) to 4 decimals returning 0.0002 instead of 0.0001. Another: 0.206 with precision 8 returns 0.20600001 etc.

I tested using two methods: 1/x (1 divided by x) and composing decimal numbers by string concatenation. Let me know if you would like me to post the code for that.

The only rounding method that I have not found any errors in is the one I posted earlier. I guess nobody tested it because they figured it was to short to be able to work correctly ;-)

So until a better method comes along the best way is: (credit for the core of this solution goes to Jack Moore. See: http://www.jacklmoore.com/notes/rounding-in-javascript/)

Code: Select all
/**
* Round using exponential notation shifting
*
* @param {Number} value
* @param {Number} precision
*/
function round(value, precision) {
   return Number(Math.round(Number(Math.abs(value)+'e'+precision))+'e-'+precision) * (value < 0 ? -1 : 1)
}
Intrasoft / JBS Group, Partner
Omar van Galen
Servoy Developer
omar
 
Posts: 316
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: another (different) rounding problem

Postby ROCLASI » Thu Sep 21, 2017 4:58 pm

Nice!
I missed that one.
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5230
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: another (different) rounding problem

Postby jcompagner » Fri Sep 22, 2017 9:37 am

omar wrote:
Code: Select all
/**
* Round using exponential notation shifting
*
* @param {Number} value
* @param {Number} precision
*/
function round(value, precision) {
   return Number(Math.round(Number(Math.abs(value)+'e'+precision))+'e-'+precision) * (value < 0 ? -1 : 1)
}



interesting that that works, it is for sure a bit slower as all the others ;)
because its quite a lot number -> to string (concat) -> number -> to string -> tonumber

So the magic here has to be in the Number constructor and the parse of the string that that constructor gets, that weird rounding errors won't happen..
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8029
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: another (different) rounding problem

Postby omar » Fri Sep 22, 2017 9:47 am

If you leave out the second Number constructor it works as well but you will get an implicit conversion anyway. I included it myself to get rid of the Servoy warning that otherwise occurs. I like to keep my solutions completely free of warnings ;-) Performance is not that bad either, during testing I did several hundred millions of round operations in a few seconds.
Intrasoft / JBS Group, Partner
Omar van Galen
Servoy Developer
omar
 
Posts: 316
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: another (different) rounding problem

Postby patrick » Fri Sep 22, 2017 10:14 am

We will add that to svyUtils/svyJSUtils so it doesn't get lost.
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3630
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Previous

Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 6 guests

cron