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