How does PHP output extra-large numbers?

Source: Internet
Author: User
Tags mul php definition pow
If there is such a number, I do not know if it is possible, but if the function is returned, there will certainly be this big number

$num = 101000000411132513120104;

Direct echo

echo $num;//或者echo (string)$sum;

have been

1.0100000041113E+23

If you perform

$num='A'.$num;

Output

A1.0100000041113E+23

How can I remember that it is output as it is in the browser?

(PS: Of course, the implementation of the situation rarely has such a large number, I just want to know that PHP has no way to solve this)

Reply content:

If there is such a number, I do not know if it is possible, but if the function is returned, there will certainly be this big number

$num = 101000000411132513120104;

Direct echo

echo $num;//或者echo (string)$sum;

have been

1.0100000041113E+23

If you perform

$num='A'.$num;

Output

A1.0100000041113E+23

How can I remember that it is output as it is in the browser?

(PS: Of course, the implementation of the situation rarely has such a large number, I just want to know that PHP has no way to solve this)

If an integer exceeds the maximum value of the current environment int after the PHP definition or assignment, PHP is going to convert the integer to float because it can be stored in scientific notation. And this process, will bring the loss of precision, that is, your $num after the assignment is not exactly the original number, so it can not be output as is.

If you want to output as-is, convert the value to string before assigning it, and store it so that it is output as-is. Of course, this is no longer an operation, because once the operation is done, it returns to the floating-point store.

That is directly stored as a string. It needs to be calculated and then converted.

You can refer to this article for a direct simulation of the calculation:

Http://www.51-n.com/t-3945-1-1.html

PHP will make an error when calculating a large numeric operation, and when the number is too large, the value will be counted as scientific. So how do you do PHP super-large numeric operations, including subtraction, exponentiation, square root, modulo operations?
To solve the problem of scientific counting, just add a pair of quotes when assigning a value.

Such as
$n = ' 22222222222222222222222222220 ';
Echo $n;
?>
If not quoted, display 2.2222222222222E+28, quote 22222222222222222222222222220

Extra-large numerical operations, including subtraction, power operations, square roots, modulo operations.

Use PHP's Bcmath function to create a custom function with the following code,
Function Calc ($m, $n, $x) {

    $errors =array (' dividend cannot be zero ', ' negative numbers have no square root ');                    Switch ($x) {case ' add ': $t =bcadd ($m, $n);            Break                    Case ' Sub ': $t =bcsub ($m, $n);            Break                    Case ' Mul ': $t =bcmul ($m, $n);            Break                    Case ' div ': if ($n!=0) {$t =bcdiv ($m, $n);                    }else{return $errors [0];            } break;                    Case ' POW ': $t =bcpow ($m, $n);            Break                    Case ' mod ': if ($n!=0) {$t =bcmod ($m, $n);                    }else{return $errors [0];            } break;                    Case ' sqrt ': if ($m >=0) {$t =bcsqrt ($m);                     }else{       return $errors [1];    } break; } $t =preg_replace ("/\.    *0+$/",", $t); return $t;

}
/ usage Examples /
Echo Calc (' 11111111111111111111111111111111110 ', ' ten ', ' Add ');
?>

How to use:
Calc (parameter 1, parameter 2, parameter 3);
Parameter 3 Specifies the method of operation: Add, Sub minus, Mul, div except, pow power, mod modulo, sqrt to find the square root of arithmetic
Add and subtract: Parameter 1 plus/minus/multiply/divide by parameter 2
Power: Parameter 1 of the parameter 2 times Square.
Modulo: The remainder of the parameter 1 divided by the parameter 2.
Arithmetic square root: The arithmetic square root of parameter 1. Parameter 2 does not work, but cannot be omitted.

PHP generally said there is no solution, you astronomical, may be useful in professional science, but PHP is not oriented to professional science, since everyone knows that PHP digital precision has limitations, you can only follow this limit to consider the problem. For the civilian, the accuracy of 13 bits is sufficient anyway.

PHP support can only be self-rewriting the kernel. (And I don't know if 64-bit operating systems are supported.) )

$num = 101000000411132513120104; After the assignment has been $num is not equal to the original value, it is not possible to store the original value and calculate, unless before the assignment is converted to character exchange

As far as I know, no, after all, the data is for people to see, users do not want to see so much 0, so with mathematical symbols can be

Directly into a string, if you want to calculate the type of re-rotating

$num='101000000411132513120104';

Can be stored in two stages

With a string.
When calculating, write the algorithm yourself.
Within 10 is always accurate.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.