<div dir="ltr"><br><br>On Mon, Oct 8, 2018 at 6:43 PM Nikos Alexandris <<a href="mailto:nik@nikosalexandris.net">nik@nikosalexandris.net</a>> wrote:<br>><br>> Dears,<br>><br>> I try to understand why the following assertion fails to work, as<br>> expected, in a Python GRASS GIS script?<br>><br>> The following fragment of code<br>> ```<br>> fraction_categories = grass.parse_command('r.category',<br>>                 map=fractions,<br>>                 delimiter='\t')<br>><br>> fractions_sum = sum([float(x)<br>>     if not math.isnan(float(x))<br>>     else 0<br>>     for x<br>>     in fraction_categories.values()])<br>> msg = "Fractions: {f}".format(f=fraction_categories)<br>> g.message(_(msg))<br>> g.message(_("Sum: {s}".format(s=fractions_sum)))<br>> ```<br>><br>> gives<br>> ```<br>> Fractions: {u'24': u'nan', u'25': u'0.0159235666814', u'26':<br>> u'0.0891719747445', u'27': u'nan', u'20': u'0.0238853509712', u'21':<br>> u'0.0286624211654', u'23': u'0.143312100132', u'29': u'nan', u'40': u'nan',<br>> u'41': u'0.0318471333627', u'1': u'nan', u'3': u'nan', u'2':<br>> u'0.0159235669187', u'5': u'nan', u'4': u'nan', u'7': u'nan', u'6': u'nan',<br>> u'9': u'nan', u'8': u'nan', u'11': u'0.00159235669186', u'10': u'nan',<br>> u'12': u'0.162420386604', u'15': u'0.366242033672', u'16': u'nan', u'18':<br>> u'0.0573248423308', u'31': u'nan', u'36': u'nan', u'35':<br>> u'0.0636942667255', u'32': u'nan'}<br>> Sum: 1.0<br>> ```<br>><br>> Yet the following assertion does not work as expected<br>> ```<br>> assert fractions_sum < 1.000000000000001, "Sum of fractions is > 1"<br>> ```<br>> and returns<br>> ```<br>> Traceback (most recent call last):<br>>   File "/osgeo/grasstrunk/dist.x86_64-pc-linux-gnu/scripts/r.estimap", line 3979, in <module><br>>     sys.exit(main())<br>>   File "/osgeo/grasstrunk/dist.x86_64-pc-linux-gnu/scripts/r.estimap", line 3964, in main<br>>     **supply_parameters)<br>>   File "/osgeo/grasstrunk/dist.x86_64-pc-linux-gnu/scripts/r.estimap", line 2864, in compute_supply<br>>     assert fractions_sum < 1.000000000000001, "Sum of fractions is > 1"<br>> AssertionError: Sum of fractions is > 1<br>> ```<br><div><br></div><div>What is the sum when printed out with %.17g?</div><div><br></div><div>Why do you use 1.000000000000001 and not e.g. 1.000001 as threshold? 1.000000000000001 is most probably a too small difference to 1 considering floating point precision limits.</div><div><br></div><div>Markus M<br></div><div><br></div><div>></div>> The following _does_ work, in Python (inside a GRASS GIS session),<br>> ```<br>> import os, sys, subprocess<br>> import datetime, time<br>> import csv<br>> import math<br>> import atexit<br>> import grass.script as grass<br>> from grass.exceptions import CalledModuleError<br>> from grass.pygrass.modules.shortcuts import general as g<br>> from grass.pygrass.modules.shortcuts import raster as r<br>> from grass.pygrass.modules.shortcuts import vector as v<br>> fractions_sum = 1.<br>> assert fractions_sum < 1.000000000000001, "Sum of fractions is > 1"<br>> ```<br>><br>> Nikos<br>> _______________________________________________<br>> grass-dev mailing list<br>> <a href="mailto:grass-dev@lists.osgeo.org">grass-dev@lists.osgeo.org</a><br>> <a href="https://lists.osgeo.org/mailman/listinfo/grass-dev">https://lists.osgeo.org/mailman/listinfo/grass-dev</a></div>