Bluehost.com Web Hosting $6.95

Floats and avoid exponential notation - How?

This is a discussion on Floats and avoid exponential notation - How? within the PHP General forums, part of the PHP Programming Forums category; Hi, From http://www.php.net/manual/en/language.types.float.php (second commentin that page, from "kjohnson at ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-17-2008
k bah
 
Posts: n/a
Default Floats and avoid exponential notation - How?


Hi,

From http://www.php.net/manual/en/language.types.float.php (second commentin that page, from "kjohnson at zootweb dot com"):

"PHP switches from the standard decimal notation to exponential notation for certain "special" floats. You can see a partial list of
such "special" values with this:"

Then he goes on about it and finishes with:

" I have to be honest: this is one of the strangest things I have seen in any language in over 20 years of coding, and it is a
colossal pain to work around."

I have the same problem. I have a big number I have to represent, it's usually "1" followed by 10 "zeros", the biggest value I'll
have for it is 19999999999, never more than this. I only make one operationwith it, (+), most of the time I need that number as a
string, and never need it's float representation, only the absolute value (in fact, it's never going to have a fractional part). I
cannot use integers because it's bigger than the integer range.

If it goes to it's exponential representation, breaks my code. Users are identified by that number. I wrote a small function, but
cannot be sure if it's going to work (report error when the exponential notation is used by php), mostly because on my tests, I
can't precise when and to which of these numbers php chooses to use the exponential notation:

--- code
function checkFloat($float_var) {

$ar_empty = "";
$string_var = (string)$float_var;

$pattern = '/[0-9]|\./'; // From zero to nine and "dots"
$match_found = preg_match_all($pattern, $string_var, $ar_empty);

unset($ar_empty);
if ($match_found != strlen($string_var)) {
return false;
} else {
return true;
}
}
--- code

So, any suggestions/thoughts?
Is there a way to prevent php from using the exponential notation for a float?


thanks

=


--
Powered by Outblaze
Reply With Quote
  #2 (permalink)  
Old 07-17-2008
Andrew Ballard
 
Posts: n/a
Default Re: [PHP] Floats and avoid exponential notation - How?

On Thu, Jul 17, 2008 at 7:23 AM, k bah <kbah@linuxmail.org> wrote:
>
> Hi,
>
> From http://www.php.net/manual/en/language.types.float.php (second comment in that page, from "kjohnson at zootweb dot com"):
>
> "PHP switches from the standard decimal notation to exponential notation for certain "special" floats. You can see a partial list of
> such "special" values with this:"
>
> Then he goes on about it and finishes with:
>
> " I have to be honest: this is one of the strangest things I have seen in any language in over 20 years of coding, and it is a
> colossal pain to work around."
>
> I have the same problem. I have a big number I have to represent, it's usually "1" followed by 10 "zeros", the biggest value I'll
> have for it is 19999999999, never more than this. I only make one operation with it, (+), most of the time I need that number as a
> string, and never need it's float representation, only the absolute value (in fact, it's never going to have a fractional part). I
> cannot use integers because it's bigger than the integer range.
>
> If it goes to it's exponential representation, breaks my code. Users are identified by that number. I wrote a small function, but
> cannot be sure if it's going to work (report error when the exponential notation is used by php), mostly because on my tests, I
> can't precise when and to which of these numbers php chooses to use the exponential notation:
>
> --- code
> function checkFloat($float_var) {
>
> $ar_empty = "";
> $string_var = (string)$float_var;
>
> $pattern = '/[0-9]|\./'; // From zero to nine and "dots"
> $match_found = preg_match_all($pattern, $string_var, $ar_empty);
>
> unset($ar_empty);
> if ($match_found != strlen($string_var)) {
> return false;
> } else {
> return true;
> }
> }
> --- code
>
> So, any suggestions/thoughts?
> Is there a way to prevent php from using the exponential notation for a float?
>
>
> thanks
>
> =
>
>
> --
> Powered by Outblaze
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Does changing the value of 'precision' in php.ini make a difference?
Mine is set to 14, and had no problem rendering that number in
standard notation.

I'm not sure what kind of problems you're having with this format, but
I think this might fix them. (Even if you pass the number in
exponential notation to a database, most databases I've worked with
will recognize it and handle it correctly.) If it's just a display
issue, will number_format($float_value, 0, '', '') work?

Andrew
Reply With Quote
  #3 (permalink)  
Old 07-17-2008
Kirk.Johnson@zootweb.com
 
Posts: n/a
Default Re: [PHP] Floats and avoid exponential notation - How?

"k bah" <kbah@linuxmail.org> wrote on 07/17/2008 05:23:40 AM:

> Hi,
>
> From http://www.php.net/manual/en/language.types.float.php (second

comment in
> that page, from "kjohnson at zootweb dot com"):
>
> "PHP switches from the standard decimal notation to exponential notation

for
> certain "special" floats.
>
> I have the same problem. I have a big number I have to represent, it's
> usually "1" followed by 10 "zeros", the biggest value I'll
> have for it is 19999999999, never more than this. I only make one

operation
> with it, (+), most of the time I need that number as a
> string, and never need it's float representation, only the absolute

value (in
> fact, it's never going to have a fractional part). I
> cannot use integers because it's bigger than the integer range.
>
> If it goes to it's exponential representation, breaks my code. Users

are
> identified by that number.
>
> So, any suggestions/thoughts?
> Is there a way to prevent php from using the exponential notation for a

float?

I don't know of any way to modify PHP's behavior with regard to this.
That's not to say there isn't a way, but I don't know of one.

This might be a brute force way to address it. You'll want to add
additional checks so you don't end up in any infinite loops. You should
definitely test with larger numbers in the range that you are actuallly
using. Hope this gets you what you need:

// example: one of the "special" ones, prints in exponential notation,
"1.4E+6"
$num = 1400000.;

if(strstr($num, 'E')) {
echo "yep, exp notation<br>";
list($significand, $exp) = explode('E', $num);
list($void, $decimal) = explode('.', "$significand");
$decimal_len = strlen("$decimal");
$exp = str_replace('+', '', "$exp");
$exp -= $decimal_len;
$append = '';
for($i = 1; $i <= $exp; $i++) {
$append .= '0';
}
$tmp = str_replace('.', '', "$significand");
$reconsctructed = "$tmp" . "$append";
echo '<pre>reconstructed: ', "$reconsctructed", '</pre>';
}

kjohnson at zootweb dot com :)
Reply With Quote
  #4 (permalink)  
Old 07-18-2008
mrclay
 
Posts: n/a
Default Re: Floats and avoid exponential notation - How?

On Jul 17, 7:23*am, k...@linuxmail.org ("k bah") wrote:
> I only make one operation with it, (+), most of the time I need that number as a
> string, and never need it's float representation, only the absolute value(in fact, it's never going to have a fractional part). I
> cannot use integers because it's bigger than the integer range.


http://php.net/manual/en/function.bcadd.php
Or without BCMath write an equivalent for integers.

Steve
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:14 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0