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

107
Visualizações
Map numbers to other numbers with interpolation in Javascript

I have a dataset with a volume for a given surface elevation of an irregular basin. For example:

cm      kL
11870 : 6043453
11871 : 6053522
11872 : 6063591
11873 : 6073674
11874 : 6083767
(...1550 rows)

cm is a series that always increments by one; The associated kL values are irregular but always increase and are never duplicated. The mapping never changes and it can be loaded/stored in any convenient format.

Does Javascript have a simple way to convert between cm and kL? Ideally with linear interpolation in both directions. Ultimately I am looking for this functionality:

cm_to_kL(11872.2); //Expect 6065607.6
kL_to_cm(6065600); //Expect 11872.199
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I wrote an example of how to start solving this problem. Like already mentioned, there are no internal functionality for interpolating or handling such structures, but you need to write your own logic.

I have to admit I'm not an expert what comes to math (+ it's 2am here, but this question got me interested in :D).

I hope this helps you at least to get started:

const data = {
  11870 : 6043453,
  11871 : 6053522,
  11872 : 6063591,
  11873 : 6073674,
  11874 : 6083767,
};

const cm_to_kL = (val) => {
    const cm_ref = Math.floor(val);
    const factor = parseFloat((val % cm_ref).toFixed(5));
    const lower = data[cm_ref];
    const higher = data[cm_ref + 1];

    if (isNaN(lower) || isNaN(higher)) {
        throw Error('Data entry point not found');
    }
    
    const result = lower + ((higher - lower) * factor);

    if (isNaN(result)) {
        throw Error('Incorrect data provided');
    }

    return result;
};

const kL_to_cm = (val) => {
    const [cm, kL] = Object.entries(data).find(([k, v]) => val < v);

    if (isNaN(cm) || isNaN(kL)) {
        throw Error('Data entry point not found');
    }
    
    const lower_cm = cm - 1;
    const lower_kL = data[lower_cm];
    const diff = (val - lower_kL) / (kL - lower_kL);

    const result = parseFloat((lower_cm + diff).toFixed(5))

    if (isNaN(result)) {
        throw Error('Incorrect data provided');
    }

    return result; 
};

console.log('11872.2',
    cm_to_kL(11872.2),
);

console.log('6065600',
    kL_to_cm(6065600),
);

about 4 years ago · Juan Pablo Isaza Relatório

0

Yes of course JS have to do what you need! You can create 2 Maps from your given array, one for cm to kL and another for kL to cm. Create two functions for them cm_to_kL and kL_to_cm to gat value from Maps after this you can easily get elements with O(1) complexity

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