Learn R Programming

roperators (version 1.3.14)

floating_point_comparisons: Floating point comparison operators

Description

These are an important set of operators missing from base R. In particular, using == on two non-interger numbers can give unexpected results (see examples.)

See this for details: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Usage

x %~=% y

x %>~% y

x %

Arguments

x

numeric

y

numeric

See Also

Other comparisons: comparisons

Examples

Run this code
 ## Floating point test of equality ####

 # Basic Equality - no roperators:
 (0.1 + 0.1 + 0.1) == 0.3   # FALSE
 # Basic Equality - with roperators:
 (0.1 + 0.1 + 0.1) %~=% 0.3 # TRUE


 # NOTE: for floating point >= and <=
 (0.1 + 0.1 + 0.1) %>=% 0.3 # TRUE
 (0.1 + 0.1 + 0.1) %<=% 0.3 # FALSE

 # Use >~ and <~ for greater/less than or approx equal
 (0.1 + 0.1 + 0.1) %>~% 0.3 # TRUE
 (0.1 + 0.1 + 0.1) %<~% 0.3 # TRUE

Run the code above in your browser using DataLab