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