NumericalOrder

NumericalOrder[e1,e2]

gives 1 if e1<e2, -1 if e1>e2, 0 if e1 and e2 are identical or numerically the same and Order[e1,e2] if e1 and e2 are not numerically comparable.

Details

  • NumericalOrder provides a general alternative to canonical order in which numeric expressions, dates and Quantity objects are treated by value, but it is otherwise equivalent to canonical order.
  • Quantity expressions with compatible units are compared to each other by magnitude after converting them to a common unit.
  • DateObject expressions are compared to each other by AbsoluteTime.
  • TimeObject expressions are compared by AbsoluteTime.
  • NumericalOrder compares inexact numbers using all available significant digits. Unlike Equal, it does not allow any extra tolerance.
  • NumericalOrder can be used as an ordering function in functions like Sort, OrderedQ or Ordering.

Examples

open allclose all

Basic Examples  (4)

These two numbers are not ordered:

These two are numerically the same:

Compare numeric expressions:

This is not always the same as the canonical order of expressions:

Compare quantities:

Compare dates:

Scope  (6)

Compare any two numeric expressions:

- comes before any real-valued expression:

comes after any real-valued expression:

Complex valued expressions are ordered first by the real part:

When the real part is numerically the same, they are ordered by the absolute value of the imaginary part:

Compare quantities of compatible units:

The comparison is performed by converting into a common unit:

DateObject expressions are ordered by AbsoluteTime:

Use NumericalOrder as ordering function:

Sort using the ordering permutation:

The resulting list is not ordered in canonical order, but it is ordered in numerical order:

Applications  (1)

Get the numerical ordering for a list:

Properties & Relations  (8)

For numeric expressions of different value, NumericalOrder compares them using those values:

Order always compares expressions structurally and may give different results:

Like Order, NumericalOrder is an antisymmetric function of expressions: NumericalOrder[e1,e2]==-NumericalOrder[e2,e1]:

Unlike Order, NumericalOrder[e1,e2] may return zero for non-identical e1, e2:

For comparable expressions e1, e2 a result NumericalOrder[e1,e2]0 implies e1-e2==0:

NumericalOrder compares inexact numbers using all available significant digits:

For machine-precision numbers, Less, Equal, Greater, etc. use 7 bits of tolerance:

Inexact numbers with any other precision are compared up to that precision:

NumericalOrder compares complex values by the real part and then by absolute value of the imaginary part:

This is consistent with Order for numbers:

Less, LessEqual and related functions cannot compare complex numbers:

Equivalent quantities have a NumericalOrder of 0:

The canonical order distinguishes between the two representations:

Use Equal to show that they are indeed equivalent quantities:

For non-numerical expressions e1, e2, NumericalOrder coincides with Order:

Possible Issues  (1)

Sorting with NumericalOrder will not guarantee a particular ordering for different representations of the same number:

This does not give the same result for a permutation of the list:

The canonical order will rearrange in a definite way:

A stricter order can be defined by using Order to resolve cases where NumericalOrder gives 0:

Introduced in 2017
 (11.1)