0

I want to compare 2 values based on which I get the color for my legend. But unfortunately I cannot use ceil, floor or round the values as it will effect my result.

I have following color table with first value as point and rest 3 values are r g b values.

Basically I get point from backend which ca be 0.4607441262895224, 0.5500956769649571 and so on. I need to compare the point with first value in color table and give corresponding color. But I am facing issue in comparing , as currently I have given to Fixed(2) but it is not recommended.

"colors": [
[ 0.00, 255, 13, 186 ],
[ 0.25, 254, 4, 135 ],
[ 0.50, 73, 255, 35 ],
[ 0.75, 185, 116, 255 ],
[ 1.00, 32, 50, 255 ]
]

// main logic
const test = arraySet.find((ele) => {
  // point is dynamic
  const point = 0.388920938
  // I cannot use toFixed(2) here, which need to be changed
  // ele[0] is the first value in array 0.00, 0.25, 0.50 and so on
  return point.toFixed(2) == ele[0].toFixed(2);}
);
Eric
  • 123
  • 9
  • why are you using toFixed() if you just want to compare them ? – Thibaud Nov 16 '21 at 11:54
  • @Saren Yes, thats wrong. I cannot use toFixed() and dont know how can I compare it – Eric Nov 16 '21 at 11:56
  • if point is a number and ele is a number you can just do this ```point === ele``` – Thibaud Nov 16 '21 at 11:58
  • point is something like 0.4607441262895224 or 0.5500956769649571 and so on dynamic values and ele can be 0.00 or 0.25 or 0.50 and so on – Eric Nov 16 '21 at 12:05
  • 2
    You can use @kikon's answer with a threshold of something like `0.125` so that every dynamic point will be matched to one of the fixed points assuming that your dynamic points are between 0 and 1 – aerial Nov 16 '21 at 12:24

3 Answers3

3

I suppose you want something on the lines of

Math.abs(point-ele[0]) < 0.01 

replace 0.01 with a threshold relevant to your data, e.g., 1e-6 for 10^(-6)

kikon
  • 3,670
  • 3
  • 5
  • 20
  • Yes, even I assume that I need some thresold but may I know what can I replace with 0.01, as I am not familiar with thresold. – Eric Nov 16 '21 at 12:06
  • Well,it depends on your values, what range of values they can take and when do you consider them to be sufficiently close to be equal – kikon Nov 16 '21 at 12:10
  • You didn't use the standard `point===ele` so I assume you wanted to consider numbers equal even if they are not perfectly equal, but close within a certain range. – kikon Nov 16 '21 at 12:17
  • How about using Math.abs(point - ele) < Number.EPSILON ? – Eric Nov 16 '21 at 12:37
  • That's not a good idea in general, for a better solution see [this post](https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison). But I suspect your differences are not only related to floating point precision. Do you but any chance want to find the `ele` in the array that is closest to `point`? – kikon Nov 16 '21 at 12:46
  • Not sure though as I am new to the project and team, need to check with other team members. Hopefully I will have answer for your question soon. – Eric Nov 16 '21 at 13:45
0

ToFixed will also be rounded

const toFixed=(num)=>Number(num.toString().match(/^\d+(?:\.\d{0,2})?/));
vueAng
  • 415
  • 2
  • 7
0

Try doing it like this

return point.toString().substr(0, ele.toString().length) == ele;