Converting long strings to decimals with more than 13 places - php

This function is converting an array which may contain strings to numbers which may or may not have a decimal.
function toNumberArray(array $stringArray) {
print_r ($stringArray);
echo "<hr>";
$n = [];
for ($i = 0; $i < count($stringArray); $i++) {
$n[] = settype ( $stringArray[$i],"float");
$x = (float) $stringArray[$i];
echo $x;
echo "<br>";
}
return $n;
}
print_r (toNumberArray(["1.123456789012345","2.2000000000000002","3.2999999999999998"]));
Results:
Array ( [0] => 1.123456789012345 [1] => 2.2000000000000002 [2] => 3.2999999999999998 )
1.1234567890123
2.2
3.3
Array ( [0] => 1 [1] => 1 [2] => 1 )
Question:
Why is settype not converting the string to a float?
How to convert if there are 14 or more places after the decimal?

It is converting to a PHP float which is a 64-bit IEEE floating point number. You are probably expecting that PHP uses a 32-bit IEEE floating point number for float and a 64-bit IEEE floating point number for a double'. But, in PHPfloatanddouble` are synonyms. (see the link in jorgonor's comment).
You could use PHP's round function to convert the 64-bit number to 6 or 7 decimal points to simulate the effect you are probably looking for. See http://php.net/manual/en/function.round.php

Related

Unexpected behaviour in an array with some values

My code is:
$arr=[02,05,07,08,09];
print_r($arr)
and output is:
Array
(
[0] => 2
[1] => 5
[2] => 7
[3] => 0
[4] => 0
)
Why it converts 08 and 09 to 0??
Numbers beginning with a zero are considered to be in base 8.
See PHP docs: Integers - Syntax.
To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.
its considered as octal number. because you are starting it from zero.
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)

Why for with float step dont do last iteration?

Why does my for loop with a float step (0.1) don't do the last iteration in PHP - any version?
My test code:
$procents_list = [];
for($i = 0.5; $i <= 1.5; $i += 0.1)
{
$procents_list[] = $i;
}
print_r($procents_list);
Iteration from 5 to 15 with step 1 all ok.
Why that print:
Array
(
[0] => 0.5
[1] => 0.6
[2] => 0.7
[3] => 0.8
[4] => 0.9
[5] => 1
[6] => 1.1
[7] => 1.2
[8] => 1.3
[9] => 1.4
)
Computers use Base-2 (0,1) instead of Base-10 (0,1,2,...9) to represent numbers. In Base-10, some numbers cannot be represented exactly (like 1/3=0.33333). In Base-2, a different set of numbers cannot be represented exactly. In fact, 1/10, which is 0.1 in decimal, has no exact representation in binary. So, math with floats can do weird things and should never be taken as exact values.
I couldn't figure out how to get PHP to print out all the digits without rounding, but writing the same example in Java prints out the following values:
0.5
0.6
0.70000005
0.8000001
0.9000001
1.0000001
1.1000001
1.2000002
1.3000002
1.4000002
So, the value you were getting was slightly larger than 1.5 and the loop thus ended.
In this case, one alternative is to just use integers for the loop and divide by 10 when putting them in the array:
$procents_list = [];
for ($i = 5; $i <= 15; $i += 1)
{
$procents_list[] = $i / 10;
}
print_r($procents_list);

php : result of sum array values is wrong

I have an array:
$test =array('49'=> '-0','51'=> '-0','50'=> '0','53'=> '-1.69','55'=> '0','57'=> '-2','59'=> '-6','60'=> '-12','65'=> '0','66'=> '0','67'=> '21.69','69'=> '0','70'=> '0','71'=> '0',);
echo "\n".'===== First Method ========';
echo "\n\n".print_r($test);
echo "\n array_sum: ".array_sum($test);
echo "\n\n".'===== Second Method ========';
$total = 0;foreach($test as $value) $total += $value;
echo "\n foreach:".$total."\n";
the result is
gd#gd:~/Desktop$ php test.php
===== First Method ========Array
(
[49] => -0
[51] => -0
[50] => 0
[53] => -1.69
[55] => 0
[57] => -2
[59] => -6
[60] => -12
[65] => 0
[66] => 0
[67] => 21.69
[69] => 0
[70] => 0
[71] => 0
)
1
array_sum: 3.5527136788005E-15
===== Second Method ========
foreach:3.5527136788005E-15
it is wrong, the result should be 0, not 3.5527136788E-15, how to fix it ?
This is just your standard floating point arithmetic precision error.
php -r "echo -1.69 + -2 + -6 + -12 +21.69;"
3.5527136788005E-15%
You can fix it by using ints rather than floats. For example, if you always expect 2 digits of precision, multiply all your numbers by 100, round them off to ints, sum them, and divide by 100.
php -r "echo (-169 + -200 -1200 +2169 + -600) / 100;"
0%
You are doing array_sum with strings. Remove the quotes on the values, or covert them to integers before using array_sum - I imagine it is converting the strings to integers incorrectly - only on my phone so can't check specifics.
Hope this helps.
Why not just give the values as floating point numbers rather than enclosing them in quotes. That would basically make it a string summation and strange result is expected. I think in before PHP version 4.something it used to convert the string to numbers. It might especially be problem with decimal numbers.
This is just an example of floating point imprecision. It's impossible to represent .69 exactly in binary (much like it's impossible to represent 1/3 exactly in decimal).
If you need exact numbers, you can look into using the bcmath php extension.

php split integer into smaller parts

i'm working on a project that will need to have everything shown with barcodes, so I've generated 7 numbers for EAN8 algorithm and now have to get these 7 numbers seperately, right now i'm using for the generation
$codeint = mt_rand(1000000, 9999999);
and I need to get this 7 numbers each seperately so I can calculate the checksum for EAN8, how can i split this integer to 7 parts, for example
12345678 to
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5
arr[5]=6
arr[6]=7
any help would be appreciated..
also I think that I'm becoming crazy :D because I already tried most of the solutions you gave me here before and something is not working like it should work, for example:
$codeint = mt_rand(1000000, 9999999);
echo $codeint."c</br>";
echo $codeint[1];
echo $codeint[2];
echo $codeint[3];
gives me :
9082573c
empty row
empty row
empty row
solved! $codeint = (string)(mt_rand(1000000, 9999999));
Try to use str_split() function:
$var = 1234567;
print_r(str_split($var));
Result:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
There are two ways to do this, one of which is reasonably unique to PHP:
1) In PHP, you can treat an integer value as a string and then index into the individual digits:
$digits = "$codeint";
// access a digit using intval($digits[3])
2) However, the much more elegant way is to use actual integer division and a little knowledge about mathematical identities of digits, namely in a number 123, each place value is composed of ascending powers of 10, i.e.: 1 * 10^2 + 2 * 10^1 + 3 * 10^0.
Consequently, dividing by powers of 10 will permit you to access each digit in turn.
it's basic math you can divide them in loop by 10
12345678 is 8*10^1 + 7*10^2 + 6*10^3...
the other option is cast it to char array and then just get it as char
Edit
After #HamZa DzCyberDeV suggestion
$string = '12345678';
echo "<pre>"; print_r (str_split($string));
But in mind it comes like below but your suggestion is better one.
If you're getting string from your function then you can use below one
$string = '12345678';
$arr = explode(",", chunk_split($string, 1, ','));
$len = count($arr);
unset($arr[$len-1]);
echo "<pre>";
print_r($arr);
and output is
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
okay what you can do is
Type cast to string with prefill 0
this is how it works
$sinteger = (string)$integer;
$arrsize = 0 ;
for (i=strlen($sinteger), i == 0 ; i--)
{
arr[$arrsize]=$sinteger[i];
$arrsize++;
}
And then what is left you can prefill with zip.
I am sure you can manage the order reverse or previous. but this is simple approach.

Calculating decimal from most significant bits & least significant bits in PHP

I'm a little stumped here. I have 2 32-bit integers, one for the most significant bits & the other is the least. How do I find the decimal equivalent from those two in PHP?
Update:
I have tried the suggestion from #bwoebi and modified it a little. Here's the code:
$value=get64($msb, $lsb);
function get64($msb, $lsb) {
$count = count($lsb);
for($i=0; $i < $count; $i++) {
$value[$i] = bcadd(bcmul($msb[$i], bcpow(2, 32)), $lsb[$i] > 0?$lsb[$i]:bcadd(bcsub(bcpow(2, 32), $lsb[$i]), 2 << 30)); // $a most significant bits, $b least significant bits
}
}
Output:
msb=Array ( [0] => INTEGER: 0 [1] => INTEGER: 0 )
lsb=Array ( [0] => INTEGER: 143 [1] => INTEGER: 0 )
value=Array ( [0] => 2147483648 [1] => 2147483648 )
The output does not look right...I would have expected 143 as value[0]. Any input?
bcadd(bcmul($a, bcpow(2, 32)), $b >= 0?$b:bcsub(bcpow(2, 32), $b)); // $a most significant bits, $b least significant bits
should do the job. I am using the bc library as there maybe imprecisions when working with normal operators on the 32 bit systems (It would be converted to float).
You can use the BC Math or GNU Multiple Precision extensions to perform arithmetic with values too large for native PHP variables.

Categories