sqrt(0.0000000185); returns 0.00013601470. This is ok
sqrt(0.0000000065); returns 8.06225774. This is wrong. It had to be 0.000080622577483
How to fix this?
Thanks
You can use the Mathematical Extensions BC Math
http://php.net/manual/en/function.bcsqrt.php
echo bcsqrt('0.0000000065',20); //Result 0.00008062257748298549
Here the number has to be an string!
Note: The basic mathematical functions of php arent that nice...
Have a nice day.
Related
I tried to do it like 5*log(7.5/2), but I think it's the wrong way, can anyone tell me how to do it right ?
In PHP, a base 10 logarithm is made using log10, not log (which is for natural logarithm i.e. base e). Note also that your comparison example in the comments is with 5.7, not 7.5. Try this instead:
echo 1+5*log10(5.7/2);
Output:
3.2742243000426
Demo on 3v4l.org
I have a number that is pulled from an API that displays as 6.5e-7, I would like to display this as 0.00000060 (I think this is what 6.5e-7 means),
I have tried to get this correct using the php ROUND function but it will only show as 0 or 6.5e-7 when I used a precision.
print round($vSat, 8);
Any help would be much appreciated
If I had to guess, I would think you need to do a formatted print which is printf
printf ("%.8f", $vSat)
%.8f means floating point and 8 decimal places
http://us3.php.net/printf
Use printf/sprintf formatting.
printf("%10.10f", $yournumber);
Trying to format a scientific number in PHP:
sprintf(%'1.2E',$var)
This gets me to 5.01E+1
I am trying to print 2 digits after the + sign
The parser requires the number format to be:
5.01E+01 instead of 5.01E+1
Is it possible to achieve this format with sprintf?
Is there any other method that can achieve this?
Thanks for looking
I couldn't find a way to do it solely with sprintf but I believe the following would be closer to the "correct" way to do it. The following calculates the exponent from the base-10 logarithm.
You can then pass the original value (dividing by 10 to the power of the exponent) and the exponent to the sprintf function as a float and an integer respectively. You can force the positive + sign remembering that it counts towards the character / padding length.
function scientificNotation($val){
$exp = floor(log($val, 10));
return sprintf('%.2fE%+03d', $val/pow(10,$exp), $exp);
}
demo / test cases :
scientificNotation(5.1); // 5.10E+00
scientificNotation(50.1); // 5.01E+01
scientificNotation(500.1); // 5.00E+02
scientificNotation(0.0051); // 5.10E-03
There is no built-in method as far as I know. But with a little bit of Regex black magic you can do something like this:
preg_replace('/(E[+-])(\d)$/', '${1}0$2', sprintf('%1.2E',$var));
Notice that I just wrapped your call to sprintf() with an appropriate call to preg_replace(). If the regular expression does not match it will leave the output from sprintf() as is.
The above answers both work great.
Also found out I can use:
$var_formatted = shell_exec("printf '%1.2E' $var");
which could be the cleanest given the script has the permissions to execute commmands.
The E+nn format is the default output format for the linux shell printf.
From the shell:
:~$ echo `printf '%1.2E' 500`
5.00E+02
:~$ echo `printf '%1.2E' 5`
5.00E+00
:~$ echo `printf '%1.2E' 0.05`
5.00E-02
I am aware that the function may be computed manually using factorials, the problem is that larger numbers will not calculate properly.
For example, if I input COMBIN(1500,5) in MS Excel it will return 62,860,358,437,800 as it should. But if I try to calculate it manually, even in Excel I will get a #NUM! error when I try to first find the factorial of 1500. The manual formula would be
1500!/5!(1500-5)!
I find it curious that Excel's COMBIN function calculates properly yet the manual way returns an error. In short, I am wondering if there is an equivalent of this function in PHP? I have already tried manually computing using gmp_fact and as with Excel, it returns error (NaN).
Thanks
Your calculation is failing because you're quickly overflowing the Integer data type, which is probably just 32-bits on your system. You could use the arbitrary precision math functions to get around that problem:
http://www.php.net/manual/en/ref.gmp.php
If gmp_fact(...) seems to be returning errors or bad results, you're probably passing it a bad value or assuming it's result is a basic numeric type. You'll want to use something like gmp_strval to convert the returned GMP resource into something readable when you're done performing calculations.
Example:
$a = gmp_fact(1500);
$b = gmp_fact(5);
$c = gmp_fact(1500-5);
$result = gmp_div($a,(gmp_mul($b,$c)));
echo gmp_strval($result);
I have a field in my mySQL db table of type decimal (15,4)
I've never experienced this before but if the value is 0.0000 my PHP if statement is returning true!
As a sanity check I even did:
if(0.0000) echo "Hello World";
And sure enough, Hello World echo'd out. What the hell is going on? Anybody got any ideas?
If it's a float value coming out from DB it will be treated like a string, not like a numeric value. You can try something this:
if(floatval($value) > 0) { ... }
Where $value contains the value from DB.
I think the problem is you got a string "0.0000" from db but not 0.0000.
Try again with:
if ((int)$your_value) echo "Hello World";
I don't have this behavior on my PHP version (5.3.3).
I suggest casting the number to bool before doing the check: if ((bool) $float).
I know it's been a long time since this question was opened but I thought it might help out someone else.
A float is actual an approximation of the Real Number. You will find that not all Real numbers can be represented by a float. This might be the reason why you are getting unexpected results. You should have a read of
http://php.net/manual/en/language.types.float.php.
If however you need higher precision you should look at using the BC Math lib's http://php.net/manual/en/ref.bc.php.