[GRASS-SVN] r52911 - grass/branches/releasebranch_6_4/lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Aug 25 08:24:21 PDT 2012
Author: mmetz
Date: 2012-08-25 08:24:20 -0700 (Sat, 25 Aug 2012)
New Revision: 52911
Modified:
grass/branches/releasebranch_6_4/lib/gis/color_write.c
Log:
gislib: rephrase comment, fix fp precision (sync to trunk)
Modified: grass/branches/releasebranch_6_4/lib/gis/color_write.c
===================================================================
--- grass/branches/releasebranch_6_4/lib/gis/color_write.c 2012-08-25 15:22:35 UTC (rev 52910)
+++ grass/branches/releasebranch_6_4/lib/gis/color_write.c 2012-08-25 15:24:20 UTC (rev 52911)
@@ -20,9 +20,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <grass/gis.h>
-#define PRECISION 30
-#define THRESHOLD .0000000000000000000000000000005
-/* .5 * 10 ^(-30) */
static int write_new_colors(FILE *, struct Colors *);
static int write_rules(FILE *, struct _Color_Rule_ *, DCELL, DCELL);
@@ -124,7 +121,7 @@
fprintf(fd, "%% %s %s\n", str1, str2);
if (colors->shift) {
- sprintf(str2, "%.10f", (double)colors->shift);
+ sprintf(str2, "%.15g", (double)colors->shift);
G_trim_decimal(str2);
fprintf(fd, "shift:%s\n", str2);
}
@@ -172,7 +169,7 @@
if (rule->low.value == dmin)
format_min(str, (double)rule->low.value);
else {
- sprintf(str, "%.10f", (double)rule->low.value);
+ sprintf(str, "%.15g", (double)rule->low.value);
G_trim_decimal(str);
}
fprintf(fd, "%s:%d", str, (int)rule->low.red);
@@ -183,7 +180,7 @@
if (rule->high.value == dmax)
format_max(str, (double)rule->high.value);
else {
- sprintf(str, "%.10f", (double)rule->high.value);
+ sprintf(str, "%.15g", (double)rule->high.value);
G_trim_decimal(str);
}
fprintf(fd, " %s:%d", str, (int)rule->high.red);
@@ -249,12 +246,16 @@
{
double dtmp;
- sprintf(str, "%.*f", PRECISION, dval);
+ sprintf(str, "%.15g", dval);
+ /* Note that G_trim_decimal() does not trim e.g. 1.0000000e-20 */
G_trim_decimal(str);
sscanf(str, "%lf", &dtmp);
- if (dtmp != dval) { /* if no zeros after decimal point were trimmed */
- sprintf(str, "%.*f", PRECISION, dval - THRESHOLD);
- /* because precision is probably higher than PRECISION */
+ if (dtmp != dval) { /* if no zeros after decimal point were trimmed */
+ /* lower dval by GRASS_EPSILON fraction */
+ if (dval > 0)
+ sprintf(str, "%.15g", dval * (1 - GRASS_EPSILON));
+ else
+ sprintf(str, "%.15g", dval * (1 + GRASS_EPSILON));
}
return 0;
@@ -264,12 +265,16 @@
{
double dtmp;
- sprintf(str, "%.*f", PRECISION, dval);
+ sprintf(str, "%.15g", dval);
+ /* Note that G_trim_decimal() does not trim e.g. 1.0000000e-20 */
G_trim_decimal(str);
sscanf(str, "%lf", &dtmp);
- if (dtmp != dval) { /* if no zeros after decimal point were trimmed */
- sprintf(str, "%.*f", PRECISION, dval + THRESHOLD);
- /* because precision is probably higher than PRECISION */
+ if (dtmp != dval) { /* if no zeros after decimal point were trimmed */
+ /* increase dval by by GRASS_EPSILON fraction */
+ if (dval > 0)
+ sprintf(str, "%.15g", dval * (1 + GRASS_EPSILON));
+ else
+ sprintf(str, "%.15g", dval * (1 - GRASS_EPSILON));
}
return 0;
More information about the grass-commit
mailing list