0

Candidates are :

  • 2 ints (one indicating scale),
  • float (used with caution when doing calculations as this type is inherently imprecise and in some cases imprecision errors can wrongfully add up),
  • BigDecimal (since it seems to be the standard in java for that type of things)

The problem with option number one is i am lazy and don't want to reinvent wheel if someone has already done it better than me.

The problem with option number two and three are : are those not overkills, and wouldn't those severely impact performance as well as memory and storage usage when those "10 or 20 bytes and little performance overhead" are multiplied by millions of stock quotes, additions, differences, and comparisons.

  • 2
    Put simply, **don't** use float or double. See https://stackoverflow.com/q/3730019/3788176 – Andy Turner Apr 04 '20 at 18:24
  • You might want to look at https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Progman Apr 04 '20 at 18:25
  • @SharonBenAsher yes, BigDecimal was my initial idea but isn't it too big a container as most number wouldn't have more than 5 or 6 significant digits ? what about speed of calculations ? –  Apr 04 '20 at 18:35
  • @whoever downvoting, please dont hesitate explaining why –  Apr 04 '20 at 18:37
  • @AndyTurner yes i am already aware that float or double have an inherent imprecision due to the fact that there is only a finite number of possible float values supposed to represent an uncountable infinite set of possible numbers, and that those imprecisions might add up –  Apr 04 '20 at 18:41
  • You seem to understand that you have a choice between (a) floating-point: small and fast and inaccurate, or (b) `BigDecimal`: large and slow and accurate. Which do you think the accountant will want you to use for tracking money? – Basil Bourque Apr 04 '20 at 19:15
  • The down-votes might be due to this subject having been covered on Stack Overflow many times already. Always search Stack Overflow thoroughly before posting. – Basil Bourque Apr 04 '20 at 19:19
  • @BasilBourque I am not working for an accountant and am not tracking money but i have to evaluate performance of rule-based trading strategies on stocks, bonds, fx, and other markets. So it seems to me float is the best compromise if i am able to carefully handle the (i guess rare) cases where imprecision would add up. –  Apr 04 '20 at 19:23
  • You could check out some libs that handle money. https://www.joda.org/joda-money/ – matt Apr 04 '20 at 19:25
  • @BasilBourque and no Why not use Double or Float to represent currency? doesn't answer my question. I am aware of what is said in the most voted answer. In fact i was more interested in the answer stating that this so called imprecision is overestimated as most of times it shoudnt be a problem once taken in consideration –  Apr 04 '20 at 19:26
  • As for being a duplicate, your Question does not provide enough specifics to advise you further. You would have to explain the details of your math for anyone to give further advice. Edit your Question with such specifics, and the Question can be reopened. – Basil Bourque Apr 04 '20 at 19:31
  • Sounds like you might benefit from both technologies. Use floating-point where speed or size matters, and accuracy does not. In other parts, such as accumulations, use `BigDecimal` where you care more about accuracy. Utilize the rounding features of `BigDecimal` to tame your intermediate floating-point results. When in doubt, use `BigDecimal`. And do not fall into the trap of premature optimization. – Basil Bourque Apr 04 '20 at 19:33
  • @nasd4q if there are answers arguing in both directions in the dupe, that's good, no? You need to evaluate the answers with respect to your specific requirements (we don't know those in sufficient detail). Also: "billions of stock quotes" perhaps try building something simpler, smaller, picking one representation; then evaluate the results. You can change your mind armed with more information. – Andy Turner Apr 05 '20 at 12:04
  • @AndyTurner lol alright, i'll change that to "millions of quotes". –  Apr 05 '20 at 16:31
  • As for the case of duplicate, a better fit would be https://stackoverflow.com/questions/354045/lightweight-java-decimal-class, which has no good answer. –  Apr 05 '20 at 16:43

0 Answers0