Add 64 bit numbers in 32-bit windows - php

The following code:
$val = "209810563658809344";
echo sprintf('%0.0f', ($val - 1) + "<br />");
echo sprintf('%0.0f', (bcsub($val, 1)) + "<br />");
In windows 32 bit outputs:
209810563658809344
209810563658809344
How can be made the subtraction to work properly?

Both your sprint() masking and the use of + instead of . for concatenation are forcing a cast to numeric datatype
$val = "209810563658809344";
echo sprintf('%0.0f', ($val - 1)) . '<br />';
echo bcsub($val, 1) . '<br />';

Avoid the conversion to floating point and you'll be fine.
$val = "20981056365880934";
echo bcmul($val, 2, 0)." ≈ ".bcdiv($val, 2, 0)." × 2 + ".bcsub($val, 1, 0)." + 1";
Note the third scale parameter which allows you to control precision like printf(). See the bc manual

Related

Round to 0.5 up and down and if 0 after dot docimal

Hey I have following Problems,
I get ratings and have the average of then but bow I want following:
1,2222222222222 = 1
1,2666666666666 = 1,5
2,3635345435435 = 2.5
2,567435 345345 = 2.5
3.5709 = 3
29,3003453450 = 29
I want all numbers to their most neir .5 but when they have a no decimal then should NOT be displayed 3.0 or 4.0, Just the number without decimals.
at the moment i have this code:
function roundRating($rating) {
return floor($rating * 2) / 2;
}
can someone help me?
greets
Here is a suggestion. See comments for step by step explanation.
<?php
$float = 1.753242342;
// Modulo operator (%) does not return float remainder. Use fmod().
$remainder = fmod($float, 1);
/* Apply rounding logic here.
* It isn't entirely clear to me what your intended behavior is.
* Should 0.75 round up to 1 or down to 0.5?
* I will leave this to you to figure out.
*/
$roundedRemainder = ($remainder >= 0.25)?0.5:0;
$int = $float - $remainder;
$result = $int + $roundedRemainder;
// If remainder is zero, cast to int to remove remainder.
if (fmod($result, 1) == 0)
$result = (int)$result;
echo "Int: " . $int . "<br>";
echo "Remainder: " . $remainder . "<br>";
echo "Rounded remainder: " . $roundedRemainder . "<br>";
echo "Result: " . $result . "<br>";
Outputs:
Int: 1
Remainder: 0.753242342
Rounded remainder: 0.5
Result: 1.5

Print a sequence of numbers in PHP with the correct (+/-) sign

I need to print some numbers in a sequence with + and - between them. However, I don't know beforehand which number is going to be positive and which is going to be negative. Currently, I echo them like this:
echo "$a + $b + $c + $d + $e + $f";
Let's say the values of $a to $f are all positive. I will get something like: 5 + 10 + 12 + 18 + 9 + 7.
However, if some of the values are negative, I will get something like 5 + -10 + 12 + -18 + 9 + - 7. The ideal output in this case would have been 5 - 10 + 12 - 18 + 9 - 7.
Please not that I don't want to calculate the final result of addition or subtraction. I just want to print it all on the webpage with correct signs.
I could do so by writing 6 nested if() blocks but that seems like a lot of work and doing it every time will be error prone. Is there anything clever that I can do to output the right sign.
The easiest way would be to correct the operator appearance in the final string:
$s = '5 + -10 + 12 + -18 + 9 + - 7'; // result of interpolation or concatenation
$s = str_replace('+ -', '- ', $s);
// => "5 - 10 + 12 - 18 + 9 - 7"
If this is possible for you, this is as fast as it gets -- no looping (in php), no treating each number one at a time with a conditional. If you loop anyway, I'd recommend #Phils suggestion with array_reduce -- functional style php -- adapted to your needs.
Need to check for each variable manually like this:
echo "$a " . ($b < 0 ? " - " . abs($b) : " + $b") . ($c < 0 ? " - " . abs($c) : " + $c") . ($d < 0 ? " - " . abs($d) : " + $d") . ($e < 0 ? " - " . abs($e) : " + $e") . ($f < 0 ? " - " . abs($f) : " + $f");
you can use php sprintf function.
function formatNum($num){
return sprintf("%+d",$num);
}
or
function formatNum($num) {
$num = (int) $num; // or (float) if you'd rather
return (($num >= 0) ? '+' : '-') . $num; // implicit cast back to string
}
for more detail please read it :- http://php.net/manual/en/function.sprintf.php
try this
$a = 10;
$b=-20;
$text = $a." ".$b;
$text= str_replace(" ", "+", $text);
echo $text;
OUTPUT
10+-20
Put the numbers in an array and use array_reduce to create the string
$numbers = [5, -10, 12, -18, 9, -7];
$first = array_shift($numbers);
echo array_reduce($numbers, function($str, $num) {
return $str . sprintf(' %s %d', $num < 0 ? '-' : '+', abs($num));
}, $first);
Demo ~ https://eval.in/1034635
This way you can handle an arbitrary amount of numbers without duplicating logic everywhere.

Addition by ZERO - PHP

Could somebody please tell me why $total_value evaluates to ZERO when either $bawtry_value or $chain_value are non-zero values?
PHP
$bawtry_value = utf8_encode(money('%n', $bawtry * $price));
echo "Bawtry Value";
echo '<br />';
echo $bawtry_value;
echo '<br />';
$chain_value = utf8_encode(money('%n', $chain * $price));
echo "Chain value";
echo '<br />';
echo $chain_value;
echo '<br />';
$total_value = utf8_encode(money('%n', $bawtry_value + $chain_value));
echo "Total value";
echo '<br />';
echo $total_value;
echo '<br />';
Example Output
Result Bawtry 0
Result Chain 2
Total Pairs 2
Bawtry Value
£0.00
Chain value
£39.90
Total value
£0.00
Is there a problem with PHP adding two figures together when one is ZERO?
Any help would be great. Thanks
It seems you should change this line:
$total_value = utf8_encode(money('%n', $bawtry_value + $chain_value));
into
$total_value = utf8_encode(money('%n', $bawtry * $price + $chain * $price));
or even
$total_value = utf8_encode(money('%n', $price * ($bawtry + $chain)));
Now in calculating $total_value you have currency symbol at the beginning of $bawtry_value and $chaing_value and if you add them, they are converted to 0.
You can see this if you try to display:
echo '£0.00' + '£39.90';
first character of those strings is £ is converted to 0.
However if you try to do something like that
echo '2£0.00' + '39.90£';
result will be 41.90 because strings are converted to int until the first character that makes the string cannot be converted to int.

unexpected result in php output with larger strings

I am trying to create a binary/hexadecimal converter to convert a denary(base 10) number/value into binary and hexadecimal.
It works fine so far for binary until the input from the form is greater than 11 digits and over(string length), ruffly as it seems to variety. after 11 digits it starts adding " - " into the outcome. Im not sure were this is coming from as I don't have an " - " in the code.
I wasn't sure if this was something to do with large integers as I saw some other questions on that topic(not in php however it was java, so not sure if there is something simpler in php)
That said I was under the impression that form inputs were always strings.
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). - from : http://www.php.net/manual/en/function.is-float.php
(haven't yet got to hexadecimal but needed to mention it as some of the following code contains parts for it.)
here is the php code (note: I do check user input just not added it yet)
$inpu = $_POST['number'];
$numinput = $_POST['number'];
if (is_numeric($numinput))
{
while ($numinput >= 1)
{
$binary .= $numinput % 2;
$numinput = $numinput / 2;
}
$mult = strlen($binary) % 4;
echo gettype($numinput) . "<br />";
echo gettype($binary) . "<br />";
echo gettype($mult) . "<br />";
echo $mult . "<br />";
while ($mult < 4)
{
$binary.= "0";
$mult++;
}
$revbinary = strrev($binary);
echo $inpu . " in binary = " . $revbinary ;
echo "<br /> <br />";
echo chunk_split($revbinary, 4);
echo "<br />" . gettype($revbinary) . "<br />";
echo gettype($inpu) . "<br />";
}
else
{
if (is_numeric($numinput))
{
echo "$numinput is over the max value of 255";
}
else
{
echo "your entry is not a vaild number <br />";
echo $numinput;
}
}
Im not looking for completed version of this code as you would ruin my fun, I am just wondering why there is a "-" being entered after 11 digits or so. It also did't add the symbol before I added :
$mult = strlen($binary) % 4;
echo $mult . "<br />";
while ($mult < 4)
{
$binary.= "0";
$mult++;
}
This was to split the binary into 4s ( 0011 1101 0010 0110 ).
Edit: wondered if this would be useful:
echo gettype($numinput); result double
echo gettype($binary); result string
echo gettype($mult); result integer
gettype($revbinary); result string
echo gettype($inpu); result string
still trying to work this out myself.
Any advice is much appreciated Thanks
I would suggest simply using decbin() and dechex(). Those are functions included in php, which do exactly what you're trying to accomplish.
You might want to check if it is a number first (like you are already doing).
Then cast it to an integer (through intval()) and then apply decbin() and dechex()
http://php.net/manual/de/function.decbin.php
http://www.php.net/manual/de/function.dechex.php
http://php.net/manual/de/function.intval.php

changing font makes + combine strings instead of adding values

Here is my code:
<?php
$num1 = 10;
$num2 = 15;
echo "<font color='red'>$num1 + $num2</font>" . "<br>";
?>
I expect it to equal "25", when I add a font color it equals "10 + 15". Why?
You cannot do math inside a string. Instead, close the string. perform your addition operation, and concatenate the result.
echo "<font color='red'>" . ($num1 + $num2) . "</font><br>";
As Orangepill wisely points out, this can be accomplished more efficiently like this:
echo "<font color='red'>",($num1 + $num2),"</font><br>";
change
echo "<font color='red'>$num1 + $num2</font>" . "<br>";
to
echo "<font color='red'>",($num1 + $num2),"</font><br>";
You need to concatenate your string otherwise math will not work
echo "<font color='red'>".($num1 + $num2)."</font>" . "<br>";

Categories