Jump to content

Floating Numbers and Integers


iwato

Recommended Posts

BACKGROUND: The following code is supposed to capture all of the floating numbers in an array defined by the expression $array = array(0.0, 0.2, 2.0, 3, 3.0, 4, 4.1).QUESTION: Although PHP treats $array[2] as a floating number the preg_grep() function with the following pattern /(\d+)?\.\d+/ does not. Why not?Does not /\d/ include any digit from 0 to 9?The OUTPUT:float(2) Array ( [1] => 0.2 [6] => 4.1 )

<?php	$array = array(0.0, 0.2, 2.0, 3, 3.0, 4, 4.1);	echo var_dump($array[2]), '<br />';	$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);	print_r($fl_array);?>

Roddy

Link to comment
Share on other sites

I assume that the array values are being interpreted as floating point numbers, rather than strings.regexps really run on strings.Therefore, I suspect if you modify the array to enclose each value in quotes, making the elements strings, it will work.

Link to comment
Share on other sites

... I suspect that if you modify the array to enclose each value in quotes, ... , it will work.
Your suspicion was correct.Changing a non-performing element to a string produced the intended result.Great observation!Thanks.Roddy
Link to comment
Share on other sites

On a side note, you might have noticed that only numbers that have a decimal part different than zero are matched. That's a side effect of the float-to-string casting (which, as noted, the preg_grep() does to each value before matching it) - A number is displayed (and stored actually) as its shortest representation that doesn't change the literally written numeric value. For example, if you also had

4.200000

The regexp will match it, but it will be seen in the returned array as

Array ( [1] => 0.2 [6] => 4.1 [7] => 4.2 )

and a simple echo would also output it as "4.2".

Link to comment
Share on other sites

On a side note, you might have noticed that only numbers that have a decimal part different than zero are matched.
No, I had not. PHP appears to discount the importance of scientific significance.Roddy
Link to comment
Share on other sites

I don't know if I would say it like that, I think it's fair to say that 4.2 === 4.20000. There are plenty of ways to format the number like you want to display it, but there's no reason to store extra zeros when they aren't needed, whether they're leading or trailing.

Link to comment
Share on other sites

I don't know if I would say it like that, I think it's fair to say that 4.2 === 4.20000.
These are not equivalent.4.2 could mean anything less than 4.15, whereas 4.20000 means only those numbers that are less than 4.200005. These two expressions are not the same in levels of significance.Roddy
Link to comment
Share on other sites

I remember my high school Maths teachers arguing about this :) (no consensus was reached and half the cohort had their tests marked differently). However, for the purpose of internal calculations done by PHP 4.2 really does mean 4.2 exactly. Visual confirmation of significance through added zeros or otherwise are nice for us humans, but PHP doesn't need to care, as it can keep track of such things without (for example, by abiding by IEEE 754).

Link to comment
Share on other sites

These are not equivalent.
Yes they are, they really are. If you want to disprove that, find a calculation where using 4.2 gives one value, and using 4.20000 gives another.
4.2 could mean anything less than 4.15
No, 4.2 means 4.2. 4.15 means 4.15. When you round then you remove significance. PHP does not round numbers unless you tell it to. It stores the number with exactly the significance it needs to reproduce it. This is what all programming languages do, this is not unique to PHP. All languages are bounded by the max floating point precision, whatever that is for the hardware they run on. Truly irrational numbers that have an infinite number of digits will always get rounded, but if a number can fit within the max precision then the value gets stored exactly as it is. This is not "PHP discounting scientific significance", this is how a computer stores a floating point number.
Link to comment
Share on other sites

Yes they are, they really are. If you want to disprove that, find a calculation where using 4.2 gives one value, and using 4.20000 gives another.
Consider the following example that I made up for the purpose of this discussion:
X = 12.215 with significance to three decimal places.(Notice here that 12.215 and 12.2150 do not mean the same.)Y = 12.2146 with significance to four decimal placesZ = 0.0016Now, X + Z = 12.227but, Y + Z = 12.2162and rounding Y + Z yields 12.216, not 12.227.This is because the lowest level of significance determines the overall significance of the numbers added.More clearly, when you add Z to X you add 0.002 to 12.215, but when you add Z to Y you add 0.0016 to 12.2146.Thus, it does appear that PHP automatically discounts scientific significance.
Roddy
Link to comment
Share on other sites

I don't know what kind of math you're studyingLogically:X+Z = 12.2166Y+Z = 12.2162There are no restrictions on decimal places in math. If a number has three significant decimal numbers then you only need to represent three digits. If a number has four significant decimal numbers then you represent four of them.PHP does not discount scientific significance, it gives the exact answers for exact numbers.

Link to comment
Share on other sites

I suppose 100 + 4 = 100 then, because there is only one significant figure for 4? Anyway, in your example above, Z only has two significant figures.Lowest common significance is used in calculations when we have values that are approximate, for example when we take measurements. However, when a number, say 2.56, is written without such a context that shows it is approximate it really is just 2.56.

Link to comment
Share on other sites

I suppose 100 + 4 = 100 then, because there is only one significant figure for 4?
Synook, this is silly. Digits to the left and right of a decimal point are not the same. Those on the right are fractions of whole numbers; those on the left are whole numbers or multiples thereof.By way of illustration 1.04 and 1.00 could represent different levels of significance depending on the power of 10 by which they are multiplied.
Anyway, in your example above, Z only has two significant figures.
I can see why your teachers could not agree, and understand now that you sided with the wrong one. In science, as opposed to mathematics or art, significance is an indicator of measurement precision. The degree of precision is measured by the order of magnitude. This is where the zero become important.
Lowest common significance is used in calculations when we have values that are approximate, for example when we take measurements.
Thinks are looking up!
However, when a number, say 2.56, is written without such a context that shows it is approximate it really is just 2.56.
No argument, but this is not my point, as I hope is now already obvious. So, now try to answer my question. How does PHP handle levels of significance?Roddy
Link to comment
Share on other sites

I don't know what kind of math you're studying.
Orders of magnitude, rounding, and measurement precision.Please see my response to Synook.Roddy
Link to comment
Share on other sites

PHP stores all non-integer numbers as double-precision floating point values. Most commonly, this means that they have a precision of around 14 decimal digits. Any such number stored in PHP automatically gains that precision.Remember, though, the IEEE 754 floating-point specification, which most platforms implement and thus PHP follows, and as I linked to above, was created to represent any decimal number as accurately as possible, and not to provide a nice system for measurement calculation. If you want, you can create an arbitrary-precision decimal type (e.g. like Java's BigDecimal class) that will do all the roundings for you, but PHP's basic numerical types are pure mathematical structures, as I said, without context. In fact, something like BigDecimal looks like exactly what you are looking for.P.S. I don't believe significance is a function of the position of the decimal point - for example, I would have represented the answer to 1×102+4×100 as 1×102, if I was, say, doing a Chemistry problem, because the lowest significance is one. However, we would of course not expect PHP to give that answer.

Link to comment
Share on other sites

In computers, everything is exact, not approximate. In real life, as well as science, everything is approximate, since that one nm, nkg, n* can (in theory) make a difference. If you take away the measurement context, and treat numbers as numbers and nothing more, then the concept of significance is lost. Instead, there's a value that is exactly that, and others which are not (even if approximately equal according to science). That's what PHP and most programming languages do.

Link to comment
Share on other sites

X = 12.215 with significance to three decimal places.(Notice here that 12.215 and 12.2150 do not mean the same.)
Are you really trying to argue that 12.215 and 12.2150 are not the same quantity? If I have 12 apples versus 12.0 apples versus 12.0000000 apples, do I have more apples in one pile than in another, or do all piles have the same quantity?I asked you to give an example of a calculation where a number like 12.123 causes a different answer than 12.1230000. You gave an example that uses completely different values (12.215 and 12.2146 are in fact different values). I challenge you to find a single example, any mathematical formula, that yields a different answer based on the number of trailing zeros. You're using rounding. Yeah, when you're rounding the order of calculations and rounds matters. But the number of trailing zeros has absolutely zero significance at all to the calculation. If you disagree with that, again, find a single example where the number of trailing zeros causes a different result for the calculation. Not 2 different values that you're rounding, but the number of trailing zeros. Trailing zeros does not matter, in any context, in any mathematical calculation. Trailing zeros never change the value. Ever. And that's a mathematical fact. Just like leading zeros. Would you say these are different values:4000044.00000000000004.000000If you think those are different, again, find a calculation where you can substitute one of those 4 values to get a different answer.
Link to comment
Share on other sites

I challenge you to find a single example, any mathematical formula, that yields a different answer based on the number of trailing zeros. You're using rounding. Yeah, when you're rounding the order of calculations and rounds matters. But the number of trailing zeros has absolutely zero significance at all to the calculation.
Please reread by replies to you, Synook, and Ingolme carefully. In science, the number of trailing zeros has important meaning as an indicator of the level of measurement precision and cannot be ignored when representing a number. The example, that I gave you, is as clear an example that I can give in the absence of a lot of chatter. Please re-examine it carefully.I have now had an opportunity to look at Synook's suggested link -- namely, Oracle's java.math.BigDecimal class. This link shows, that Synook has finally understood the problem.Now, I repeat -- differently with each new piece of information -- my question, "Does PHP have anything similar to the Oracle's java.math.BigDecimal class?"Roddy
Link to comment
Share on other sites

If you just want to display numbers with a certain precision, and not worry about whether two numbers with trailing zeros are different in a calculation, one way is to use the number_format function to display the number with a certain precision. Another way is to save it as a string instead of a number, and convert back and forth when doing calculations. You could also use printf or sprintf to print it however you want. The round function offers several ways to round a number and there are also floor and ceil functions. It sounds like you're talking about displaying numbers with a certain precision, not trying to force PHP to use 12.0000 in a calculation instead of 12.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...