Public Functions for Comparing  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

Public Functions for Comparing

Postby orad » 28 Dec 2014

Is there any reason I should use any of the value comparison methods

PublicFunctions.DoubleEquals()
PublicFunctions.DoubleGreater()
PublicFunctions.DoubleGreaterEquals()
PublicFunctions.DoubleLess()
PublicFunctions.DoubleLessEquals()

instead of C# operators: ==, >, >=, <, <=

I suppose the only reason for this redundancy is to get compiler verification on the parameters, i.e. lval and rval both have to be of type 'Double' or you will get a compiler error. Is that correct, or are there other reasons for it?

Thanks!

AlphaCat
Posts: 69
Joined: 09 Jul 2013
Been thanked: 9 times

Re: Public Functions for Comparing  [SOLVED]

Postby AlphaCat » 29 Dec 2014

Yes, there is a reason - google for "floating point problem" or read:

http://docs.oracle.com/cd/E19957-01/806 ... dberg.html

In short, double cant store for example 0.1 precisely, so == will often fail when you dont expect it to fail, whereas these double compare functions will work as you probably intend.

User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

Re: Public Functions for Comparing

Postby orad » 29 Dec 2014

I typically use type 'decimal' (System.Decimal) for anything that has to do with money. It is less prone to the floating point problem, but currently MultiCharts.NET does not support it as Input type. When I use decimal type for an input I get:

"The property 'xyz' with attribute "InputAttribute" must be a primitive type or System.String and must have public getter and public setter."

I think MultiCharts.NET should also use 'decimal' type throughout the APIs, anywhere that monetary amounts are inputted or outputted and in all calculations.

User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

Re: Public Functions for Comparing

Postby orad » 25 Jan 2015

Issue MC-1842: System.Double is not the recommended C# type for monetary calculations

Also see this related StackOverflow question: http://stackoverflow.com/q/693372/450913


Return to “MultiCharts .NET”