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

149
Visualizações
How to make artificial underflow/ overflow?

I'd like to clamp calculations in the range of 0-360 in my program. I know if you have an 8-bit integer adding 255 + 1 will overflow to 0 and likewise, 0 - 1 will underflow to 255. I wrote this function as a test:

//only for negative numbers
function clampTo360 (a) {
  if (a < 0) {
    var b = 361 + a;
    if (b > 0) {
      return b;
    } else {
      clampTo360(b);
    }
  }
}

Is there a better way to code a function so the calculation 0 - 1 will return 360?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could add 360 for getting a positive value and return the remainder.

function clampTo360(g) {
    const rest = g % 360;
    return rest < 0 ? 360 + rest : rest
}

console.log(clampTo360(365));  //   5
console.log(clampTo360(-5));   // 355
console.log(clampTo360(700));  // 340
console.log(clampTo360(-700)); //  20

about 4 years ago · Juan Pablo Isaza Relatório

0

You can make use of the modulo operator here. However, underflowing inputs will result in negative values.

To avoid that you can add 360 to get a number in the interval [0; 720), and apply the modulo operator again to get a result between 0 and 360:

function clampTo360(g) {
    return (g % 360 + 360) % 360
}
console.log(clampTo360(720))
console.log(clampTo360(360))
console.log(clampTo360(-360))
console.log(clampTo360(-720))

about 4 years ago · Juan Pablo Isaza Relatório
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