0

I am working with data that has the following form:

/* initial value */
  xi:-8$ 

/* intermediate value */
  a:-0.2$ 

/* final value */
  xf:8$

/* step size */
  dx:0.1$ 

/* first list of values */
  x1:makelist(i,i, xi, a, dx)$

/* second list of values */
  x2:makelist(i, i, a, xf, dx)$

Then,

is(equal(last(x1), first(x2)));

returns false.

I suppose the simplest way to avoid this issue would be to define dx = 1/10, in the given example; however, I was wondering what strategies can be used, if one doesn't have that control over the data?

I considered something like is(equal( floor(last(x1)*10)/10, floor(first(x1)*10)/10 )); however, it doesn't seem like that approach would generalize well to cases where one does not know, a priori, the order of magnitude of the quantity.

Is there some version of equal for floats that checks only to a certain decimal precision? I checked the documentation for both "Functions for Numbers" and "equal" but didn't see anything that would apply (AFAICT).

Rax Adaam
  • 790
  • 3
  • 11
  • [There is no general solution for comparing floating-point numbers that contain errors from previous operations.](https://stackoverflow.com/a/21261885/298225) When operations round results, information is lost. It is **gone**, and it cannot be recovered by clever comparisons. What to do to avoid that or deal with it is application-specific; there is no best practice. – Eric Postpischil Oct 17 '21 at 23:21
  • Thank you. I do understand that the issue is general; I guess I assumed that, given how common it must be, there would be standard strategies, and therefore, perhaps, built-in functions (e.g. `float_equal(val1, val2, precisicion)`; or `equal_up_to_error(val1, val2, error)`; or perhaps some function that converts a value to a nearby rational value, up to some error tolerance; or even just some `fpprec` related setting: I would've thought that setting `fpprec` would affect the results here, but they do not); anyhow, I gather from your reply that this is not the case. Thank you for your time. – Rax Adaam Oct 18 '21 at 00:05

0 Answers0