Browse Source

util: double_find_precision function to identify ideal precision for a fp number

Luke Dashjr 11 years ago
parent
commit
bc3f735e56
2 changed files with 13 additions and 0 deletions
  1. 10 0
      util.c
  2. 3 0
      util.h

+ 10 - 0
util.c

@@ -13,6 +13,7 @@
 
 
 #include "config.h"
 #include "config.h"
 
 
+#include <math.h>
 #include <stdbool.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdio.h>
@@ -1421,6 +1422,15 @@ double tdiff(struct timeval *end, struct timeval *start)
 }
 }
 
 
 
 
+int double_find_precision(double f, const double base)
+{
+	int rv = 0;
+	for ( ; floor(f) != f; ++rv)
+		f *= base;
+	return rv;
+}
+
+
 int utf8_len(const uint8_t b)
 int utf8_len(const uint8_t b)
 {
 {
 	if (!(b & 0x80))
 	if (!(b & 0x80))

+ 3 - 0
util.h

@@ -721,6 +721,9 @@ struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_
 #define _SNP(...)  _SNP2(snprintf, __VA_ARGS__)
 #define _SNP(...)  _SNP2(snprintf, __VA_ARGS__)
 
 
 
 
+extern int double_find_precision(double, double base);
+
+
 #define REPLACEMENT_CHAR (0xFFFD)
 #define REPLACEMENT_CHAR (0xFFFD)
 #define U8_DEGREE "\xc2\xb0"
 #define U8_DEGREE "\xc2\xb0"
 #define U8_MICRO  "\xc2\xb5"
 #define U8_MICRO  "\xc2\xb5"