xref: /freebsd/contrib/bmake/unit-tests/cond-cmp-numeric-le.mk (revision 988758838533b24f5893b36514fe2c68a3f911a2)
1*98875883SSimon J. Gerraty# $NetBSD: cond-cmp-numeric-le.mk,v 1.3 2023/09/07 05:36:33 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for numeric comparisons with the <= operator in .if conditions.
42c3632d1SSimon J. Gerraty
52c3632d1SSimon J. Gerraty# When both sides are equal, the <= operator always yields true.
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.else
142c3632d1SSimon J. Gerraty.  error
152c3632d1SSimon J. Gerraty.endif
162c3632d1SSimon J. Gerraty
172c3632d1SSimon J. Gerraty.if 2 <= 1
182c3632d1SSimon J. Gerraty.  error
192c3632d1SSimon J. Gerraty.endif
202c3632d1SSimon J. Gerraty
212c3632d1SSimon J. Gerraty# If this comparison were character-based instead of numerical, the
222c3632d1SSimon J. Gerraty# 5 would be >= 14 since its first digit is greater.
232c3632d1SSimon J. Gerraty.if 5 <= 14
242c3632d1SSimon J. Gerraty.else
252c3632d1SSimon J. Gerraty.  error
262c3632d1SSimon J. Gerraty.endif
272c3632d1SSimon J. Gerraty
282c3632d1SSimon J. Gerraty.if 14 <= 5
292c3632d1SSimon J. Gerraty.  error
302c3632d1SSimon J. Gerraty.endif
312c3632d1SSimon J. Gerraty
322c3632d1SSimon J. Gerraty# Scientific notation is supported, as per strtod.
332c3632d1SSimon J. Gerraty.if 2e7 <= 1e8
342c3632d1SSimon J. Gerraty.else
352c3632d1SSimon J. Gerraty.  error
362c3632d1SSimon J. Gerraty.endif
372c3632d1SSimon J. Gerraty
382c3632d1SSimon J. Gerraty.if 1e8 <= 2e7
392c3632d1SSimon J. Gerraty.  error
402c3632d1SSimon J. Gerraty.endif
412c3632d1SSimon J. Gerraty
422c3632d1SSimon J. Gerraty# Floating pointer numbers can be compared as well.
432c3632d1SSimon J. Gerraty# This might be tempting to use for version numbers, but there are a few pitfalls.
442c3632d1SSimon J. Gerraty.if 3.141 <= 111.222
452c3632d1SSimon J. Gerraty.else
462c3632d1SSimon J. Gerraty.  error
472c3632d1SSimon J. Gerraty.endif
482c3632d1SSimon J. Gerraty
492c3632d1SSimon J. Gerraty.if 111.222 <= 3.141
502c3632d1SSimon J. Gerraty.  error
512c3632d1SSimon J. Gerraty.endif
522c3632d1SSimon J. Gerraty
532c3632d1SSimon J. Gerraty# When parsed as a version number, 3.30 is greater than 3.7.
542c3632d1SSimon J. Gerraty# Since make parses numbers as plain numbers, that leads to wrong results.
552c3632d1SSimon J. Gerraty# Numeric comparisons are not suited for comparing version number.
562c3632d1SSimon J. Gerraty.if 3.30 <= 3.7
572c3632d1SSimon J. Gerraty.else
582c3632d1SSimon J. Gerraty.  error
592c3632d1SSimon J. Gerraty.endif
602c3632d1SSimon J. Gerraty
612c3632d1SSimon J. Gerraty.if 3.7 <= 3.30
622c3632d1SSimon J. Gerraty.  error
632c3632d1SSimon J. Gerraty.endif
642c3632d1SSimon J. Gerraty
65*98875883SSimon J. Gerraty# Numeric comparison works by parsing both sides
662c3632d1SSimon J. Gerraty# as double, and then performing a normal comparison.  The range of double is
672c3632d1SSimon J. Gerraty# typically 16 or 17 significant digits, therefore these two numbers seem to
682c3632d1SSimon J. Gerraty# be equal.
692c3632d1SSimon J. Gerraty.if 1.000000000000000001 <= 1.000000000000000002
702c3632d1SSimon J. Gerraty.else
712c3632d1SSimon J. Gerraty.  error
722c3632d1SSimon J. Gerraty.endif
732c3632d1SSimon J. Gerraty
742c3632d1SSimon J. Gerratyall:
752c3632d1SSimon J. Gerraty	@:;
76