mysql_float_to_double.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Keyur Govande <kgovande@gmail.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef MYSQL_FLOAT_TO_DOUBLE_H
  17. #define MYSQL_FLOAT_TO_DOUBLE_H
  18. #include "main/php.h"
  19. #include <float.h>
  20. #include "main/snprintf.h"
  21. #define MAX_CHAR_BUF_LEN 255
  22. #ifndef FLT_DIG
  23. # define FLT_DIG 6
  24. #endif
  25. /*
  26. * Convert from a 4-byte float to a 8-byte decimal by first converting
  27. * the float to a string (ignoring localization), and then the string to a double.
  28. * The decimals argument specifies the precision of the output. If decimals
  29. * is less than zero, then a gcvt(3) like logic is used with the significant
  30. * digits set to FLT_DIG i.e. 6.
  31. */
  32. static inline double mysql_float_to_double(float fp4, int decimals) {
  33. char num_buf[MAX_CHAR_BUF_LEN]; /* Over allocated */
  34. if (decimals < 0) {
  35. zend_gcvt(fp4, FLT_DIG, '.', 'e', num_buf);
  36. } else {
  37. snprintf(num_buf, MAX_CHAR_BUF_LEN, "%.*F", decimals, fp4);
  38. }
  39. return zend_strtod(num_buf, NULL);
  40. }
  41. #endif /* MYSQL_FLOAT_TO_DOUBLE_H */