0

When I calculate the average of 2 numbers that have many decimal places, then the average limits the decimal places. I don't want to limit the decimal places. I want to stop the limit. How to stop the limit?

I tried calculating the average of 2 numbers that have many decimal places, I attached more decimal places to the 2 numbers then still it limits the average.

Here is the image showing that the average limits decimal places: Here is the image showing that the average limits decimal places I want to stop the limit.

Agrim Singh
  • 499
  • 2
  • 13
  • 2
    [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551). Also, the problem is not in the average: JavaScript can't even represent the numbers that you input; both of your numbers are cut down to about 16 useful digits before they are even summed. Try just typing `1.23456789012345678901234567890123456789` into JavaScript console (without any calculation) to see how much JavaScript can remember. Use a decimal library such as [decimal.js](https://github.com/MikeMcl/decimal.js) for arbitrary-precision decimal calculation if you need such precision. – Amadan Jun 21 '23 at 01:40
  • 1
    What do you mean by "stop the limit"? – Ricky Mo Jun 21 '23 at 01:46
  • The average doesn't limit decimal places. First of all, it is not decimal (it is just a decimal output); so you mean that accuracy is limited by average. Which it is not. The problem is just that there is no such number as `52.3624209482094820209248209840923482948204820248204809248` in javascript (and, in any mainstream language, at least without a specific library to handle arbitrary precision). Or, to be more accurate, that number is the same as `52.36242094820948`. So it doesn't matter what function you call with it, that function cannot guess digits that are not in the number. – chrslg Jun 21 '23 at 01:48
  • I don't want to use a library, is there a way to stop the limit of decimal places? – codejavascript Jun 21 '23 at 01:50
  • Just `console.log(52.3624209482094820209248209840923482948204820248204809248)`, or even just type `52.3624209482094820209248209840923482948204820248204809248` in the interactive console instead of `average(...)` and you see that this is not at all specific to `average` function. Just the fact that PC (native types in CPU, and therefore native types in most language that want to use directly CPU ops) don't have the precision you seem to expect – chrslg Jun 21 '23 at 01:51
  • 1
    No, there isn't. – chrslg Jun 21 '23 at 01:51
  • @RickyMo it means "stop the limitation of decimal places". – codejavascript Jun 21 '23 at 01:51
  • 2
    No. If you want arbitrary precision, you have to use library like [decimal.js](https://github.com/MikeMcl/decimal.js/) or [math.js](https://github.com/josdejong/mathjs), or a lightweight option [big.js](https://github.com/MikeMcl/big.js/), otherwise you will end up building your own library anyway. – Ricky Mo Jun 21 '23 at 01:51
  • Yet, but "stop the limitation of decimal places" means nothing. There is no limitation of decimal places. – chrslg Jun 21 '23 at 01:52
  • @chrslg well, I think the OP is talking about wanting infinite decimal place precision. – Pointy Jun 21 '23 at 02:20
  • 1
    @Pointy I get what OP is talking about. But their "stop the limit means stop the limit" was way to abrupt and condescending, considering that they, obviously, don't really know what they are talking about. Anyway. Computers don't have infinite precision. So whatever "stop the limit" means, point is, there is no way the result of `average` can have that precision, considering that even input numbers don't really have it. There are some libraries that allows computation with arbitrary (but not infinite) precision. That is, to use the special vocabulary, those library don't "stop the limit", ... – chrslg Jun 21 '23 at 02:25
  • 1
    ... they just allow to "push back the limit". But still, there is a limit, even if we can set it as far as we want (as long as we are ready to pay the computing and memory price). But without library, answer is plain and simple: no, there isn't any way to "stop the limit" whatever that means. – chrslg Jun 21 '23 at 02:26
  • Yea I mean it's a good idea to pick up on a vibe around people having no idea what you're talking about, agreed. – Pointy Jun 21 '23 at 02:29

0 Answers0