0

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
4
  • No, there really no basic data structure that allows two way mapping alone. So you'd need to write your own. (Also it looks like array of "kL" values is all you need with couple lines to do linear interpolation between subsequent values or indexes - O(1) for cm_to_kL, O(log n) for kL_to_cm) Commented Mar 9, 2022 at 23:19
  • Hi there, maybe I don't understand well what you're asking for...you need to convert from one unit to other or you have both unit yet and you need to find the corresponding one starting from other? I.e. I know both cm and kl yet, and I want a function that find the corresponding cm if I insert kl as parameter and vice versa? Commented Mar 9, 2022 at 23:49
  • Unless this is an assignment, I'd suggest using an equation to convert between the two units. But I'm not sure if "cm" should be cm^3, or if kL should be kilo-litre or some other measure. Commented Mar 10, 2022 at 0:13
  • @DavidThomas the data seem to be just empirical (my guess it is lake depth to total volume, measured in cm and Kilo Liters correspondingly), no formula would exist for that. Commented Mar 10, 2022 at 0:23

2 Answers 2

1

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),
);

Sign up to request clarification or add additional context in comments.

1 Comment

The way I read it it looks like I need to lookup the exact kL value for the floor of the matching cm key as well as the value for the next key, then use the cm mantissa to interpolate between the "lower" & "higher" kL values. For kL_to_cm the only difference is the lookup, but since it always increases we just look for the first value greater than the lookup value as well as the key immediately below it. This is pretty much what I expected, it just felt like surely there was some kind of map function to at least lookup the nearest values and keys. Thanks for the comments!
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

3 Comments

@NiaBassey if you need exact code example, then please edit your answer and show in which format you will get input data
As a general recommendation I'd suggest that an answer should always offer a code example to demonstrate implementation (but it is, of course, your call).
@DavidThomas I always try to add some code but in this situation I couldn't understand what is the input, 2 arrays with same length or 1 object with keys and values so I decided to wait answer, and then give an algorithm to solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.