Viewed   465 times

When i make the following multiplication in PHP:

$ret = 1.0 * 0.000000001;

i get the result: 1.0E-9

I want to convert this result into the normal decimal notation, how can i do this?

sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?

 Answers

5

sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?

sprintf works, however you miss some point here.

0.000000 is not overflow. It's just that sprintf for the %f modifier uses 6 digits per default. Also please take care that %f is locale aware, %F is probably better suited.

You might want to use more digits, e.g. let's say 4 000 000 (four million):

$ php -r "printf('%.4000000F', 1*0.000000001);"

Notice: printf(): Requested precision of 4000000 digits was truncated to PHP maximum of 53 digits in Command line code on line 1

Call Stack:
    0.0001     319080   1. {main}() Command line code:0
    0.0001     319200   2. printf() Command line code:1

0.00000000100000000000000006228159145777985641889706869

As this example shows, there is not only a common value (6 digits) but also a maximum (probably depended on the computer system PHP executes on), here truncated to 53 digits in my case as the warning shows.

Because of your question I'd say you want to display:

0.000000001

Which are nine digits, so you need to write it that way:

sprintf('%.9F',$ret)

However, you might want to do this:

rtrim(sprintf('%.20F', $ret), '0');

which will remove zeroes from the right afterwards:

0.000000001

Hope this is helpful.

Tuesday, August 30, 2022
2

That is because, actually, bcmath doesn't support scientific notation. It isn't mentioned in manuals, but as you can see, in it's implementation argument conversion is used, it's named php_str2num:

static void php_str2num(bc_num *num, char *str TSRMLS_DC)
{
    char *p;

    if (!(p = strchr(str, '.'))) {
        bc_str2num(num, str, 0 TSRMLS_CC);
        return;
    }

    bc_str2num(num, str, strlen(p+1) TSRMLS_CC);
}

and so bc_str2num:

bc_str2num (bc_num *num, char *str, int scale TSRMLS_DC)
{
  int digits, strscale;
  char *ptr, *nptr;
  char zero_int;

  /* Prepare num. */
  bc_free_num (num);

  /* Check for valid number and count digits. */
  ptr = str;
  digits = 0;
  strscale = 0;
  zero_int = FALSE;
  if ( (*ptr == '+') || (*ptr == '-'))  ptr++;  /* Sign */
  while (*ptr == '0') ptr++;            /* Skip leading zeros. */
  while (isdigit((int)*ptr)) ptr++, digits++;   /* digits */
  if (*ptr == '.') ptr++;           /* decimal point */
  while (isdigit((int)*ptr)) ptr++, strscale++; /* digits */
  if ((*ptr != '') || (digits+strscale == 0))
    {
      *num = bc_copy_num (BCG(_zero_));
      return;
    }

  /* Adjust numbers and allocate storage and initialize fields. */
  strscale = MIN(strscale, scale);
  if (digits == 0)
    {
      zero_int = TRUE;
      digits = 1;
    }
  *num = bc_new_num (digits, strscale);

  /* Build the whole number. */
  ptr = str;
  if (*ptr == '-')
    {
      (*num)->n_sign = MINUS;
      ptr++;
    }
  else
    {
      (*num)->n_sign = PLUS;
      if (*ptr == '+') ptr++;
    }
  while (*ptr == '0') ptr++;            /* Skip leading zeros. */
  nptr = (*num)->n_value;
  if (zero_int)
    {
      *nptr++ = 0;
      digits = 0;
    }
  for (;digits > 0; digits--)
    *nptr++ = CH_VAL(*ptr++);

  /* Build the fractional part. */
  if (strscale > 0)
    {
      ptr++;  /* skip the decimal point! */
      for (;strscale > 0; strscale--)
    *nptr++ = CH_VAL(*ptr++);
    }
}

-not hard to see that it will fail on scientific notation (well-commented). Perhaps documentation needs to be updated (to mention that implicitly).

Possible solution will be to convert your string to plain view before applying bcmath functions

Thursday, November 3, 2022
2

Finally I do it by hand:

public static String parseToCientificNotation(double value) {
        int cont = 0;
        java.text.DecimalFormat DECIMAL_FORMATER = new java.text.DecimalFormat("0.##");
        while (((int) value) != 0) {
            value /= 10;
            cont++;
        }
        return DECIMAL_FORMATER.format(value).replace(",", ".") + " x10^ -" + cont;
}
Monday, October 31, 2022
3

You aren't doing anything wrong. Floats are notoriously innaccurate. From the docs (In the huge red warning box):

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Friday, October 14, 2022
 
tami
 
2

Example 1

Those values will be the same -- you assign the same decimal literal to each variable. Compare that to this code:

$num1 = 27.64;
$num2 = 10.0 + 2.88 + 2.88 + 2.88 + 9.0; //In decimal arithmetic adds to 27.64

if ($num1 == $num2) {
    echo 'Good!';
} else {
    echo 'Bad!';
}

// Echo's "Bad!"

$num2 looks like it should be 27.64, but it really adds to something like 27.639999999999997015720509807579219341278076171875 (that's what I get when I do that calculation in Visual C++ on my machine). $num1 = 27.6400000000000005684341886080801486968994140625 (on my machine), so they differ.

Example 2

The trailing 0 makes no difference.

Example 3

The numbers are not within the floating-point "tolerance" so of course will differ.

Example 4

12.5 is exactly representable in floating point, so 12.5 + 12.5 is too (0.5 is 2^-1).

Wednesday, November 2, 2022
 
celos
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :