bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The sqrt function in awk is not working properly


From: Peter Brooks
Subject: The sqrt function in awk is not working properly
Date: Tue, 28 Jan 2025 13:39:51 +0000

This looks like a systematic error in the sqrt function. This matters to
me, because I'm trying to find perfect squares.

$ awk --version
GNU Awk 5.2.2, API 3.2
Copyright (C) 1989, 1991-2023 Free Software Foundation.

Running on:

> Sequoia
> ProductName: macOS
> ProductVersion: 15.2
> BuildVersion: 24C101


On a Mac Mini M2 2023

Code:

        {
>         OFMT="%f0.0"
>         for (i=1;i<90000000000;i++)
>                 {
>                 cube_sum=i^3+(i+1)^3+(i+2)^3;
>                 root_cube_sum=sqrt(cube_sum);
>                 diff = cube_sum -  (root_cube_sum * root_cube_sum);
>                 if (diff != 0 )
>                         print("error in square: " cube_sum " != "
> (root_cube_sum * root_cube_sum) " difference: " diff);
>                 }
>         }


Since the square root squared should be the original number, this should
never produce any output. However, I get answers like:

error in square: 27023300151285407744  != 27023300151285411840  difference:
> 4096
> error in square: 27023417040868745216  != 27023417040868749312
>  difference: 4096
> error in square: 27023456004138090496  != 27023456004138094592
>  difference: 4096
> error in square: 27023494967444893696  != 27023494967444897792
>  difference: 4096
> error in square: 27023533930789146624  != 27023533930789150720
>  difference: 4096
> error in square: 27023611857590009856  != 27023611857590013952
>  difference: 4096

and

> error in square: 198539625926453854208  != 198539625926453821440
>  difference: 32768
> error in square: 198539773178016825344  != 198539773178016792576
>  difference: 32768
> error in square: 198539920429652574208  != 198539920429652606976
>  difference: 32768
> error in square: 198540214933142536192  != 198540214933142568960
>  difference: 32768
> error in square: 198540803940996186112  != 198540803940996218880
>  difference: 32768
> error in square: 198540951193141608448  != 198540951193141575680
>  difference: 32768
> error in square: 198541687454960844800  != 198541687454960812032
>  difference: 32768


Any idea what the problem might be?

Regards,
Peter



-- 
Peter Brooks

Author Page: amazon.com/author/peter_brooks


reply via email to

[Prev in Thread] Current Thread [Next in Thread]