Is there any background round operation for floats?

Yavuz RA 20 Reputation points
2024-09-04T14:58:43.98+00:00

select ceiling(1285.0992 / 1.94712)

result for the query above is 660 but the query below gives 661 as a result.

declare @var1 float = 1285.0992

declare @var2 float = 1.94712

select ceiling(@var1 / @var2)

can anyone tell me how that is?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,049 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,660 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 67,096 Reputation points
    2024-09-04T15:14:42.3333333+00:00

    First floating point arithmetic is approximate. In the first example, the literal 1285.0992 / 1.94712 does not specify a precision and probably uses a different precision (7 digits) rather than the float runtime operation (15 digits).

    try:

    select ceiling(1285.0992 / 1.94712),   
           ceiling((cast(1285.0992 as float(53)) / cast(1.94712 as float(53))))
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.