How to do math function like 5log(7.5/2) in php? - php

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

Related

Php sqrt function returns wrong number

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.

php scientific notation format

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

perl unary ~ gives not the expected result

I'm trying to port a php algorithm to perl but I struggle with one bit operator I'm not familiar with...
so the php code looks like this:
...
$var = '348492634';
print ~$var;
...
result: -348492635
doing the exact same thing in perl:
...
$var = '348492634';
print ~$var;
...
result: 18446744073361058981
I read a lot about the integer size depending on the architecture of the cpu, but I never found a working solution. Maybe I'm just using the wrong function in perl...
It's necessary for the logic to get same result as in the php script.
Thanks in advance
Seems that on your setup, PHP ints are 32bit signed while perl ints are 64bit unsigned.
This will probably do what you need on the given system but it is not guaranteed to work the same if you use it on another installation of perl.
$var = '348492634'; #hex!
print ~($var - 2**32) - 2**32;
The following will do for both $var='348492634' (which you claim to have) and $var=348492634 (which you did have):
unpack('l', ~pack('l', $var))
The quick and dirty conversion is:
print -($var+1); # like ~$var in PHP
If your perl is using 64-bit integers, this will only fail for $var=-18446744073709551616
(0x8000000000000000), which is a value you wouldn't use in 32-bit PHP anyway.

PHP rand(10000000000000,9999999999999) returning "-114888782"

So, I have a PHP script:
<?
rand(1000000000000,9999999999999);
The expected result is a number with 13 digits.
But it's returning some weird numbers, as:
987419207
1032717476
-455563764
Does anyone know what's going on?
PHP: 5.2.17
OS: Tested on Debian Squeeze and Windows 7, both 64 bits
Solution (workaround)
<?
echo rand(10000,99999).rand(10000000,99999999);
Use getrandmax() to see the max value that you can get from rand(), its clearly a overflow problem.
you could use 2 of this int and make a longer one, calling rand for a 6 digit and again for a 7 digits, just an idea.
i think 10000000000000 its not a valid integer!
output
getrandmax();
Use a bignum library like BCMath or GMP. GMP is newer and seems to have a better API but that's just my opinion

Round function php

Maybe the question is simple, but I can't find the answer.
What I need to do is to round number to 2 places after comma.
Im using this:
round(($data/$count*100), 2)
And when I get number like:
60.36036036036012 and : 37.83783783783808 is OK, because it's: 60.36 and 37.84
But why this:
1.8018018018018036
Is rounded to this:
1.8000000000000003
How to round always to 2 places, after comma?
You should get 1.8 unless you use something like old PHP version with some sort of related bugs. Still, if you want to see 1.80 you need to format output string, otherwise trailing zero will be stripped by default. The most flexible approach would be to use sprintf() formatting, like this:
$val = 1.8000000000000003;
printf("%.02f", round( $val, 2 ));
which would produce
1.80
The key is "%.02f" which means you want to format (f)loating point value, with two digits after dot, padded with 0 when needed (like this case).
See the sprintf() docs for more about available formatting possibilites.
Use PHP NumberFormatter class http://es.php.net/manual/es/class.numberformatter.php

Categories