Using Microsoft SQL Server 2019
SQL evaluates @number as 0 in the following query:
DECLARE
@number int,
@engine int = 49
DECLARE
@quantity float = 1.0 / CAST(@engine AS FLOAT)
SELECT
@quantity * @engine
SELECT
@number = @quantity * @engine
SELECT
@number
Results of Query :
I don't understand why this is. All numbers 1 - 48 are giving me the expected result of 1. The only other @engine input I've found that results in a 0 is 98. I've found a workaround so I don't need a solution for this issue right now, I just want to understand why only specific numbers do this.
I understand the issue with floating point numbers in computers. But it would make sense that this issue would happen with other larger numbers like 48 or 53 for example. The floating point issue does not explain why this happens in only certain situations that match this pattern.
This only happens when the @engine variable is a multiple of 7 in some way.
7 * 7 = 49
7 * 14 = 98
7 * 28 = 196 (also results in a 0 for this query)
7 * 56 = 392 (also 0)
7 / 7 = 1
14 / 7 = 2
28 / 7 = 4
56 / 7 = 8
This goes on and on. Multiply 7 by 16 and multiply that result again by 7 you'll get another number that results in 0 with this query. Every other number between gives me the expected result of 1.