Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
How does Javascript's number precision affect large values?

So I have a project where I am dealing with both really small numbers and really large numbers. Essentially I have a counter that is counting up from 10^-9 or so. Eventually, it reaches 1 and continues counting upwards by a really small factor, but very quickly.

This counter represents a "currency" in my project. So when the user presses a button, if the "cost" associated with that button is lower than the currency, the cost is subtracted from the user's currency and they are given that item. Here's what I mean:

if (currency >= itemCost) {
        items++
        currency = currency - itemCost
        itemCost = itemCost * 2

So for example, at the beginning of the game, an example value of currency and itemCost would be 0.000000003020 or so. When a cost is subtracted at this level, the program works as intended. However, when I pass 1, it ~SOMETIMES~, stops working and sometimes it doesn't. Although it will sometimes keep working until it breaks at some arbitrary quantity. At this point, it will also call the above function if itemCost is more than the currency (at least according to their corresponding labels), leaving the user with a negative currency.

I am wondering if this is happening because the number is too long to represent both the digits greater than 1 and the long string of decimal digits, like 2347.23482348982483 for example. Is there a limit to how many digits I can accurately represent in Javascript? Would I be okay with rounding the number to say 6 decimal digits after passing 1, or do I need to look into modules that help me with the precision that I need?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda