I have a script that does some math and makes an excel sheet but i randomly get a
Warning: Division by zero in myfile.php on line 170
Which throws off my header() changes.. I say that its random because it will work one time but if i refresh the page it breaks the most confusing part is that i also have a check if its zero here is the code
167 if($cartonCount > 0){
168 echo "-----" . $cartonCount . "-----";
169 $mellow = $qty/$cartonCount;
170 $leftovers = $qty % $cartonCount;
171 for($x = 1 ; $x <= $mellow ; $x++){
If the carton count is 0 it shouldn't run at all yet i still get the warning.. if anyone has an idea let me know here is a sample of the out put
-2.38----------7.63----------12----------10----------7.5---------
7.5----------4.5----------4.5----------4.5----------4.5----------7.5-
---------1----------9.5----------7.5----------2.38----------0.06-----
Warning: Division by zero in /nfs/c08/h02/mnt/122022/domains/superstructs.com/html/catalog/test/samples/upsExport.php on line 170
-----2.38----------7.63----------7.5----------7.5----------2.38------
----0.06-----
Then, I'm putting this in as answer:
It might be because the value is NULL which, in fact, is not >0. But it will still give you that error
I notice that the place where it breaks is when $cartonCount is "0.06". I suspect that $cartonCount is being stored as a string, possibly with the European style 0,06 decimal notation. When PHP tries to convert that to a number, it sees it as a zero. I'm not sure why it is acting differently for the modulo operator and not for division or equality checks, but putting $cartonCount = intval($cartonCount) before line 167 should make things act consistently (although it might end up interpreting your 0.06 as a zero).
The modulo operator converts (or rounds) the second number to an integer. 0.06 results in 0 which creates the warning.
Possible options are for example casting to integer or round() before checking > 0, round up using ceil() or check > 1.
Based on the context ceil() seems to be the best option.
Related
I have a number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.
When I Try get remainder, It gives invalid value. I am trying to get remained of two decimal values and I get
3.4694469519536E-18
My Values are
$x=0.1; $y=0.005;
I tried the following:
echo $ed = fmod(0.1,0.005); OutPut:3.4694469519536E-18
echo $ed = ((floatval(0.1)) % (floatval(0.005)));
But getting
Warning: Division by zero
But Reality is 0.1 will be divide by 0.005 in 20 times. i.e. 0.1/0.005 = 20
The same function returns correct value when I use 10%1 = 0.
How can I get the exact remainder. ( I don't face the same issue in JQuery :) )
Check an Work Around: PHP Division Float Value WorkAround
The % operator just works on integers, so it truncates your floats to integers and tries to do 0 % 0, which results in a division by zero.
The only thing you can use is fmod(). The issue you face here is the typical floating point imprecision (the value is "nearly" zero). I'd consider to just ignore values smaller then 10^-15. There's no real alternative to this in PHP.
What you could try is doing the real division, then, with a bit of luck traces of the imprecision disappear. (like round(0.1 / 0.005) === 0.1 / 0.005)
I am new to php and was doing some mathematics and found this weird thing happening.
$numOfDiffChars = 62;
$id = 285355773910;
$remainder = $id % $numOfDiffChars;
echo "Remainder: ".$remainder." ID: ".$id." NumOfElems: ".$numOfDiffChars." ",($id - floor($id/$numOfDiffChars)*$numOfDiffChars);
The answer is as follows:
Remainder: 10 ID: 285355773910 NumOfElems: 62 26
which states that % operator gives the remainder 10 whereas mathematically its 26. What could be the reason for this? Is it just some error that I committed or is there a logic?
I am not quite sure, but if your using a 32-bit machine the problem could be that the integer $id is out of bound and therefore interpreted as a floating point.
http://php.net/manual/en/language.types.integer.php
have you tried fmod function find example on --->
http://www.php.net/manual/en/function.fmod.php
From php.net
Note that operator % (modulus) works just with integers (between -214748348 and 2147483647) while fmod() works with short and large numbers.
Modulus operator is supposed to show the remainder. Like for echo(34%100) outputs 34. But why do i get a "Division by zero" error for this code echo(34%4294967296)
4294967296 is 2^32 and cannot be represented as 32 bit number - it wraps back to 0. If you use 64-bit version of PHP, it may work.
You might be able to use floating point modulus fmod to get what you want without overflowing.
https://bugs.php.net/bug.php?id=51731
2^31 is the largest integer you can get on Windows.
If you still want to mod large numbers, use bcmod.
I found this question searching for "division by zero error when using modulus", but the reason why was different.
Modulus (the % operator) will not work when the denomenator is is less than 1. using fmod() solves the problem.
Example:
$num = 5.1;
$den = .25;
echo ($num % $den);
// Outputs Warning: Division by zero
echo fmod($num, $den);
// Outputs 0.1
$num = 5.1;
$den = 1;
echo ($num % $den);
// Outputs 0, which is incorrect
echo fmod($num, $den);
// Outputs 0.1, which is correct
There's a lot of reports of mod getting wonky with large integers in php. Might be an overflow in the calculation or even in that number itself which is going to give you bugs. Best to use a large number library for that. Check out gmp or bcmath.
When i make the following multiplication in PHP:
$ret = 1.0 * 0.000000001;
i get the result: 1.0E-9
I want to convert this result into the normal decimal notation, how can i do this?
sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?
sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?
sprintf works, however you miss some point here.
0.000000 is not overflow. It's just that sprintf for the %f modifier uses 6 digits per default. Also please take care that %f is locale aware, %F is probably better suited.
You might want to use more digits, e.g. let's say 4 000 000 (four million):
$ php -r "printf('%.4000000F', 1*0.000000001);"
Notice: printf(): Requested precision of 4000000 digits was truncated to PHP maximum of 53 digits in Command line code on line 1
Call Stack:
0.0001 319080 1. {main}() Command line code:0
0.0001 319200 2. printf() Command line code:1
0.00000000100000000000000006228159145777985641889706869
As this example shows, there is not only a common value (6 digits) but also a maximum (probably depended on the computer system PHP executes on), here truncated to 53 digits in my case as the warning shows.
Because of your question I'd say you want to display:
0.000000001
Which are nine digits, so you need to write it that way:
sprintf('%.9F',$ret)
However, you might want to do this:
rtrim(sprintf('%.20F', $ret), '0');
which will remove zeroes from the right afterwards:
0.000000001
Hope this is helpful.
You need to add precision specifier (how many decimal digits should be displayed for floating-point numbers). Something like this:
echo sprintf('%.10f',$ret); // 0.0000000010
If you have no idea what number you should specify, just give it a big number and combine it with rtrim().
echo rtrim(sprintf('%.20f', $ret), '0'); // 0.000000001
The code above will strip any 0's from the end of the string.
I suggest the use BCMath for more accuracy when you are calculating with decimal numbers. That makes sure that you actually get the results you want.
To print what you want, you should specify the precision and use %.9f, since it defaults to displaying 6 decimal numbers. That makes it something like this (just like bsdnoobz already said):
sprintf('%.9f',$ret);
To align to your system's settings and limitations, you could use serialize_precision to get the most accurate result possible.
echo rtrim(sprintf('%.'.ini_get('serialize_precision').'f', $ret));
I do not recommend using the non-locale aware %F since your question only makes sense for display purposes. Respecting locale makes for a better UX.