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 
17 #ifndef MYSQL_FLOAT_TO_DOUBLE_H
18 #define MYSQL_FLOAT_TO_DOUBLE_H
19 
20 #include "main/php.h"
21 #include <float.h>
22 #include "main/snprintf.h"
23 
24 #define MAX_CHAR_BUF_LEN 255
25 
26 #ifndef FLT_DIG
27 # define FLT_DIG 6
28 #endif
29 
30 /*
31  * Convert from a 4-byte float to a 8-byte decimal by first converting
32  * the float to a string (ignoring localization), and then the string to a double.
33  * The decimals argument specifies the precision of the output. If decimals
34  * is less than zero, then a gcvt(3) like logic is used with the significant
35  * digits set to FLT_DIG i.e. 6.
36  */
mysql_float_to_double(float fp4,int decimals)37 static inline double mysql_float_to_double(float fp4, int decimals) {
38 	char num_buf[MAX_CHAR_BUF_LEN]; /* Over allocated */
39 
40 	if (decimals < 0) {
41 		zend_gcvt(fp4, FLT_DIG, '.', 'e', num_buf);
42 	} else {
43 		snprintf(num_buf, MAX_CHAR_BUF_LEN, "%.*F", decimals, fp4);
44 	}
45 
46 	return zend_strtod(num_buf, NULL);
47 }
48 
49 #endif /* MYSQL_FLOAT_TO_DOUBLE_H */
50