[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master b7f09b88: Library (type.h): using %e for float
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master b7f09b88: Library (type.h): using %e for floats smaller than precision |
Date: |
Thu, 10 Nov 2022 05:47:51 -0500 (EST) |
branch: master
commit b7f09b8892ceaa4f7ed1768e35f24132e91ce1d6
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Library (type.h): using %e for floats smaller than precision
Until now, in 'gal_type_to_string' we used '%.6f' and '%.14f' for 32-bit
and 64-bit floats respectively. This would cause in numbers that are
smaller than those powers to be printed as zero! For example 1e-8 as a
32-bit floats would be printed as '0.000000'!
With this commit, when the number to convert to a string of characters is
less than 1.0, this function will use '%e', otherwise, it will use '%f'
like before.
This bug was reported by Elham Saremi.
This fixes bug #63340.
---
NEWS | 3 +++
lib/type.c | 20 ++++++++++++++++----
2 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/NEWS b/NEWS
index 0188e91e..d7421177 100644
--- a/NEWS
+++ b/NEWS
@@ -71,6 +71,9 @@ See the end of the file for license conditions.
0.19). Reported by Giulia Golini.
bug #63285: Table not printing anything in rows of integer columns with
value of zero.
+ bug #63340: Statistics printing zero for values smaller than 1e-6 and
+ 1e-14 for 32-bit or 64-bit floats respectively. Reported by
+ Elham Saremi.
diff --git a/lib/type.c b/lib/type.c
index 1ca0b49f..ad58d107 100644
--- a/lib/type.c
+++ b/lib/type.c
@@ -393,6 +393,8 @@ gal_type_bit_string(void *in, size_t size)
char *
gal_type_to_string(void *ptr, uint8_t type, int quote_if_str_has_space)
{
+ float *f=ptr;
+ double *d=ptr;
char *c, *str=NULL;
switch(type)
{
@@ -430,11 +432,21 @@ gal_type_to_string(void *ptr, uint8_t type, int
quote_if_str_has_space)
/* We aren't using '%g' for floating points because it can remove
statisically significant digits in some scenarios and its result is
- generally not easily predictable (can be fixed-point or
- exponential). The most conservative format is therefore '%f'. */
- case GAL_TYPE_FLOAT32: TO_STRING( float, "%.6f" ); break;
- case GAL_TYPE_FLOAT64: TO_STRING( double, "%.14f" ); break;
+ generally not easily predictable: can be fixed-point or exponential
+ depending on printed length! But the printed length of a number can
+ hide statisical significance. Therefore, the most conservative
+ format is '%Nf' (where 'N' is the number of digits after the decimal
+ point; depending on the precision of the type). But in this case,
+ when the number is smaller than 1.0, '%.Nf' will loose precision!
+ For example '0.0001234567' will be printed as '0.000123'! Thefore
+ when the value is less than 1.0, we should use %Ne (with the same
+ number of digits after the decimal point). */
+ case GAL_TYPE_FLOAT32:
+ TO_STRING( float, *f<1.0 ? "%.6e" : "%.6f" ); break;
+ case GAL_TYPE_FLOAT64:
+ TO_STRING( double, *d<1.0f ? "%.14e" : "%.14f"); break;
+ /* Unknown type! */
default:
error(EXIT_FAILURE, 0, "%s: type code %d not recognized",
__func__, type);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnuastro-commits] master b7f09b88: Library (type.h): using %e for floats smaller than precision,
Mohammad Akhlaghi <=