1*6a7405f5SSimon J. Gerraty# $NetBSD: cond-cmp-numeric-eq.mk,v 1.8 2024/08/06 18:00:16 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for numeric comparisons with the == operator in .if conditions. 42c3632d1SSimon J. Gerraty 52c3632d1SSimon J. Gerraty# This comparison yields the same result, whether numeric or character-based. 62c3632d1SSimon J. Gerraty.if 1 == 1 72c3632d1SSimon J. Gerraty.else 82c3632d1SSimon J. Gerraty. error 92c3632d1SSimon J. Gerraty.endif 102c3632d1SSimon J. Gerraty 112c3632d1SSimon J. Gerraty# This comparison yields the same result, whether numeric or character-based. 122c3632d1SSimon J. Gerraty.if 1 == 2 132c3632d1SSimon J. Gerraty. error 142c3632d1SSimon J. Gerraty.endif 152c3632d1SSimon J. Gerraty 162c3632d1SSimon J. Gerraty.if 2 == 1 172c3632d1SSimon J. Gerraty. error 182c3632d1SSimon J. Gerraty.endif 192c3632d1SSimon J. Gerraty 202c3632d1SSimon J. Gerraty# Scientific notation is supported, as per strtod. 212c3632d1SSimon J. Gerraty.if 2e7 == 2000e4 222c3632d1SSimon J. Gerraty.else 232c3632d1SSimon J. Gerraty. error 242c3632d1SSimon J. Gerraty.endif 252c3632d1SSimon J. Gerraty 262c3632d1SSimon J. Gerraty.if 2000e4 == 2e7 272c3632d1SSimon J. Gerraty.else 282c3632d1SSimon J. Gerraty. error 292c3632d1SSimon J. Gerraty.endif 302c3632d1SSimon J. Gerraty 312c3632d1SSimon J. Gerraty# Trailing zeroes after the decimal point are irrelevant for the numeric 322c3632d1SSimon J. Gerraty# value. 332c3632d1SSimon J. Gerraty.if 3.30000 == 3.3 342c3632d1SSimon J. Gerraty.else 352c3632d1SSimon J. Gerraty. error 362c3632d1SSimon J. Gerraty.endif 372c3632d1SSimon J. Gerraty 382c3632d1SSimon J. Gerraty.if 3.3 == 3.30000 392c3632d1SSimon J. Gerraty.else 402c3632d1SSimon J. Gerraty. error 412c3632d1SSimon J. Gerraty.endif 422c3632d1SSimon J. Gerraty 4398875883SSimon J. Gerraty# Numeric comparison works by parsing both sides 442c3632d1SSimon J. Gerraty# as double, and then performing a normal comparison. The range of double is 452c3632d1SSimon J. Gerraty# typically 16 or 17 significant digits, therefore these two numbers seem to 462c3632d1SSimon J. Gerraty# be equal. 472c3632d1SSimon J. Gerraty.if 1.000000000000000001 == 1.000000000000000002 482c3632d1SSimon J. Gerraty.else 492c3632d1SSimon J. Gerraty. error 502c3632d1SSimon J. Gerraty.endif 512c3632d1SSimon J. Gerraty 52e2eeea75SSimon J. Gerraty# Because an IEEE 754 double can only hold integers with a mantissa of 53 53e2eeea75SSimon J. Gerraty# bits, these two numbers are considered the same. The 993 is rounded down 54e2eeea75SSimon J. Gerraty# to the 992. 55e2eeea75SSimon J. Gerraty.if 9007199254740993 == 9007199254740992 56e2eeea75SSimon J. Gerraty.else 57e2eeea75SSimon J. Gerraty. error 58e2eeea75SSimon J. Gerraty.endif 59e2eeea75SSimon J. Gerraty# The 995 is rounded up, the 997 is rounded down. 60e2eeea75SSimon J. Gerraty.if 9007199254740995 == 9007199254740997 61e2eeea75SSimon J. Gerraty.else 62e2eeea75SSimon J. Gerraty. error Probably a misconfiguration in the floating point environment, \ 63e2eeea75SSimon J. Gerraty or maybe a machine without IEEE 754 floating point support. 64e2eeea75SSimon J. Gerraty.endif 65956e45f6SSimon J. Gerraty 66956e45f6SSimon J. Gerraty# There is no = operator for numbers. 67*6a7405f5SSimon J. Gerraty# expect+1: Malformed conditional '!(12345 = 12345)' 68956e45f6SSimon J. Gerraty.if !(12345 = 12345) 69956e45f6SSimon J. Gerraty. error 70956e45f6SSimon J. Gerraty.else 71956e45f6SSimon J. Gerraty. error 72956e45f6SSimon J. Gerraty.endif 73956e45f6SSimon J. Gerraty 74956e45f6SSimon J. Gerraty# There is no === operator for numbers either. 75*6a7405f5SSimon J. Gerraty# expect+1: Malformed conditional '!(12345 === 12345)' 76956e45f6SSimon J. Gerraty.if !(12345 === 12345) 77956e45f6SSimon J. Gerraty. error 78956e45f6SSimon J. Gerraty.else 79956e45f6SSimon J. Gerraty. error 80956e45f6SSimon J. Gerraty.endif 81