floatval.phpt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --TEST--
  2. Testing floatval() and its alias doubleval() Functions
  3. --FILE--
  4. <?php
  5. echo "*** Testing floatval() with valid float values ***\n";
  6. // different valid float values
  7. $valid_floats = array(
  8. 0.0,
  9. 1.0,
  10. -1.0,
  11. 1.234,
  12. -1.234,
  13. 1.2e3,
  14. -1.2e3,
  15. 10.0000000000000000005,
  16. 10.5e+5,
  17. 1e5,
  18. -1e5,
  19. 1e-5,
  20. -1e-1,
  21. 1e+5,
  22. -1e+5,
  23. 1E5,
  24. -1E5,
  25. 1E+5,
  26. -1E+5,
  27. .5e+7,
  28. -.5e+7
  29. );
  30. /* loop to check that floatval() recognizes different
  31. float values, expected output:float value for valid floating point number */
  32. foreach ($valid_floats as $value ) {
  33. var_dump( floatval($value) );
  34. }
  35. echo "\n*** Testing doubleval() with valid float values ***\n";
  36. /* loop to check that doubleval() also recognizes different
  37. float values, expected output:float value for valid floating point number */
  38. foreach ($valid_floats as $value ) {
  39. var_dump( doubleval($value) );
  40. }
  41. echo "\n*** Testing floatval() on non floating types ***\n";
  42. // get a resource type variable
  43. $fp = fopen (__FILE__, "r");
  44. fclose($fp);
  45. $dfp = opendir ( __DIR__ );
  46. closedir($dfp);
  47. // other types in an array
  48. $not_float_types = array (
  49. -2147483648, // max negative integer value
  50. 2147483648, // max positive integer value
  51. $fp, // resource
  52. $dfp,
  53. "0.0", // string
  54. "1.0",
  55. "-1.3e3",
  56. "bob-1.3e3",
  57. "10 Some dollars",
  58. "10.2 Some Dollars",
  59. "10.0 dollar" + 1,
  60. "10.0 dollar" + 1.0,
  61. "",
  62. true,
  63. NULL,
  64. null,
  65. );
  66. /* loop through the $not_float_types to see working of
  67. floatval() on non float types, expected output: float value valid floating point numbers */
  68. foreach ($not_float_types as $type ) {
  69. var_dump( floatval($type) );
  70. }
  71. echo "\n*** Testing doubleval() on non floating types ***\n";
  72. /* loop through the $not_float_types to see working of
  73. doubleval() on non float types, expected output: float value valid floating point numbers */
  74. foreach ($not_float_types as $type ) {
  75. var_dump( doubleval($type) );
  76. }
  77. echo "\nDone\n";
  78. ?>
  79. --EXPECTF--
  80. *** Testing floatval() with valid float values ***
  81. float(0)
  82. float(1)
  83. float(-1)
  84. float(1.234)
  85. float(-1.234)
  86. float(1200)
  87. float(-1200)
  88. float(10)
  89. float(1050000)
  90. float(100000)
  91. float(-100000)
  92. float(1.0E-5)
  93. float(-0.1)
  94. float(100000)
  95. float(-100000)
  96. float(100000)
  97. float(-100000)
  98. float(100000)
  99. float(-100000)
  100. float(5000000)
  101. float(-5000000)
  102. *** Testing doubleval() with valid float values ***
  103. float(0)
  104. float(1)
  105. float(-1)
  106. float(1.234)
  107. float(-1.234)
  108. float(1200)
  109. float(-1200)
  110. float(10)
  111. float(1050000)
  112. float(100000)
  113. float(-100000)
  114. float(1.0E-5)
  115. float(-0.1)
  116. float(100000)
  117. float(-100000)
  118. float(100000)
  119. float(-100000)
  120. float(100000)
  121. float(-100000)
  122. float(5000000)
  123. float(-5000000)
  124. *** Testing floatval() on non floating types ***
  125. Warning: A non-numeric value encountered in %s on line %d
  126. Warning: A non-numeric value encountered in %s on line %d
  127. float(-2147483648)
  128. float(2147483648)
  129. float(5)
  130. float(6)
  131. float(0)
  132. float(1)
  133. float(-1300)
  134. float(0)
  135. float(10)
  136. float(10.2)
  137. float(11)
  138. float(11)
  139. float(0)
  140. float(1)
  141. float(0)
  142. float(0)
  143. *** Testing doubleval() on non floating types ***
  144. float(-2147483648)
  145. float(2147483648)
  146. float(5)
  147. float(6)
  148. float(0)
  149. float(1)
  150. float(-1300)
  151. float(0)
  152. float(10)
  153. float(10.2)
  154. float(11)
  155. float(11)
  156. float(0)
  157. float(1)
  158. float(0)
  159. float(0)
  160. Done