Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare numbers 1

Status
Not open for further replies.

alan147

Technical User
Nov 15, 2002
128
GB
Good afternoon

Is there a way to compare two numbers such that if the two are within 5 of each other the result is true but if the difference is greater the 6 then the result is false?

Thanks

Alan
 
Hi

Code:
[teal](([/teal] [navy]difference[/navy][teal]=[/teal][navy]first[/navy]-[navy]second[/navy] [teal]))[/teal] [gray]# calculate the difference[/gray]
[teal](([/teal] [navy]${difference/#-}[/navy][teal]<[/teal][purple]6[/purple] [teal]))[/teal] [teal]&&[/teal] echo [green][i]'YES'[/i][/green] [teal]||[/teal] echo [green][i]'no'[/i][/green] [gray]# test the difference's absolute value[/gray]
Tested with Bash and MKsh.

For a working test example you may check this in Bash ( sorry, to lazy to write Ksh-friendly [tt]while[/tt] loop now ) :
Bash:
[b]for[/b] [teal](([/teal][navy]i[/navy][teal]=[/teal]-[purple]5[/purple][teal];[/teal][navy]i[/navy][teal]<=[/teal][purple]5[/purple][teal];[/teal][navy]i[/navy][teal]++));[/teal] [b]do[/b] [b]for[/b] [teal](([/teal][navy]j[/navy][teal]=[/teal]-[purple]5[/purple][teal];[/teal][navy]j[/navy][teal]<=[/teal][purple]5[/purple][teal];[/teal][navy]j[/navy][teal]++));[/teal] [b]do[/b] [teal](([/teal] [navy]d[/navy][teal]=[/teal][navy]i[/navy]-[navy]j[/navy] [teal]));[/teal] [b]printf[/b] [green][i]'%3d %3d %s[/i][/green][lime][i]\n[/i][/lime][green][i]'[/i][/green] [green][i]"$i"[/i][/green] [green][i]"$j"[/i][/green] [green][i]"$( (( ${d/#-}<6 )) && echo 'YES' || echo 'no' )"[/i][/green][teal];[/teal] [b]done[/b][teal];[/teal] [b]done[/b]


Feherke.
[link feherke.github.com/][/url]
 
Feherke

That's great just what I wanted to do.

Thanks

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top