Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Runtime warnings for overflow when squaring directly but not when breaking down in two steps #5609

Closed
ar-siddiqui opened this issue Apr 13, 2022 · 2 comments

Comments

@ar-siddiqui
Copy link

ar-siddiqui commented Apr 13, 2022

Expected behaviour and actual behaviour.

I expect this GDAL calc expression to work without warnings the same way it does when I break it down into two.

This returns RuntimeWarning: overflow encountered in square :

gdal_calc.bat --overwrite --calc "(A-(0.2*B*7))**2" --format GTiff --type Float32 --NoDataValue -9999.0 -A C:\Users\asiddiqui\Desktop\gdal_square_issue\A.tif --A_band 1 -B C:\Users\asiddiqui\Desktop\gdal_square_issue\B.tif --B_band 1 --overwrite --outfile C:\Users\asiddiqui\Desktop\gdal_square_issue\C.tif

These two, which are the broken down version of the same expression, works fine without warnings.

gdal_calc.bat --overwrite --calc "(A-(0.2*B*7))" --format GTiff --type Float32 --NoDataValue -9999.0 -A C:\Users\asiddiqui\Desktop\gdal_square_issue\A.tif --A_band 1 -B C:\Users\asiddiqui\Desktop\gdal_square_issue\B.tif --B_band 1 --overwrite --outfile C:\Users\asiddiqui\Desktop\gdal_square_issue\C.tif

gdal_calc.bat --overwrite --calc "C**2" --format GTiff --type Float32 --NoDataValue -9999.0 -C C:\Users\asiddiqui\Desktop\gdal_square_issue\C.tif --C_band 1 --overwrite --outfile C:\Users\asiddiqui\Desktop\gdal_square_issue\D.tif

image

Steps to reproduce the problem.

Run the above expressions on the attached two tifs
gdal_square_issue.zip

Operating system

Windows

GDAL version and provenance

3.4.1

@DaveEslinger
Copy link

Apparently not an exponentiation issue per se. Note that the same error occurs when multiplying the initial expression by itself.

E:\GIS\Testing>gdal_calc.bat --overwrite --calc "(A-(0.2*B*7))*(A-(0.2*B*7))" --format GTiff --type Float32 --NoDataValue -9999.0 -A A.tif --A_band 1 -B B.tif --B_band 1 --overwrite --outfile E.tif

image

@rouault
Copy link
Member

rouault commented Jun 11, 2022

Documented behaviour. See https://gdal.org/programs/gdal_calc.html#gdal-calc
"""" Despite the datatype set using --type, when doing intermediate aritmethic operations using operands of the same type, the operation result will honor the original datatype. This may lead into unexpected results in the final result. """"
and
"""" In the previous example, beware that if A and B inputs are of the same datatype, for example integers, you may need to force the conversion of one of the operands before the division operation.

gdal_calc.py -A input.tif -B input2.tif --outfile=result.tif --calc="(A.astype(numpy.float64) + B) / 2"
""""

@rouault rouault closed this as completed Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants