I've got strange problem with <f:convertNumber>. When i set maxIntegerDigits to 15 and enter, say, 16 of "1"s, i get correct number with fifteen "1"s, but if i enter 20 of "1"s i get not a number with fifteen "1"s but a number with 11 "1"s and 4 "0"s, i.e. 111111111110000, and the more digits i enter the less significant numbers and the more zeros i get. Why?
Here's code snippet:
<a4j:region>
<h:inputText id="numValue" value="#{MyBean.valueN}">
<a4j:support event="onchange" limitToList="true" ajaxSingle="true" reRender="numValue"/>
<f:convertNumber groupingUsed="false" type="number" maxIntegerDigits="15" minFractionDigits="0"/>
</h:inputText>
</a4j:region>
MyBean.valueN is java.lang.Double.
That's the nature of IEEE 754 floating point numbers. The largest value a double can hold is 1.7976931348623157e308 with that integer precision. Anything beyond this integer precision will be rounded.
You want to use the java.math.BigDecimal instead. This way your <f:convertNumber> will work as intented.
Related
I want to set precision of a long float number to 2 decimal places. Rounding the number, truncating, floor,etc. won't work. For example my number is 5.196152422706632 . I want it as 5.19 and not 5 or 5.2,etc. I want to simply remove all other digits. How should i do it?
In Excel while Calculating =SIN(PI()) formula it returns 1.22515E-16.If the PI() Value(3.14159265358979) is directly given like =SIN(3.14159265358979), it returns 3.23114E-15.
Please anyone can share your opinion about how the excel calculates differently, when 'PI' and 3.14159265358979 are passed as parameter.
Let's have the following example:
A2 is formula =PI().
A3 is value 3.14159265358979.
A5 is value copied from A2 and then paste-special: Values only.
Formula in column B is =SIN(A2) ... =SIN(A5).
So what is happening here?
While Microsoft justifies the truncating values to 15 digits with using double floating point precision according IEEE 754, this is not the whole truth. According IEEE 754 the possible count of decimal digits is not exactly 15 but 15.95 in average. So there are more digits possible in some cases. And if so, Excel stores up to 17 digits in its files although it shows only 15 digits in its sheet views and also only 15 digits can be input in its sheet views.
So =PI() will result in 3.1415926535897931 exactly and this value will also be stored. But manual input can only be 3.14159265358979. But if you copy/paste-special:Values the result of =PI(), then also 3.1415926535897931 will be stored although only 3.14159265358979 is shown.
Since *.xlsx files are simply ZIP archives, we can unzip them and look at /xl/worksheets/sheet1.xml. There we will find:
<row r="2" spans="1:2" x14ac:dyDescent="0.25">
<c r="A2" s="1">
<f>PI()</f>
<v>3.1415926535897931</v>
</c>
<c r="B2">
<f>SIN(A2)</f>
<v>1.22514845490862E-16</v>
</c>
</row>
<row r="3" spans="1:2" x14ac:dyDescent="0.25">
<c r="A3" s="1">
<v>3.14159265358979</v>
</c>
<c r="B3">
<f>SIN(A3)</f>
<v>3.2311393144413003E-15</v>
</c>
</row>
<row r="5" spans="1:2" x14ac:dyDescent="0.25">
<c r="A5" s="1">
<v>3.1415926535897931</v>
</c>
<c r="B5">
<f>SIN(A5)</f>
<v>1.22514845490862E-16</v>
</c>
</row>
q.e.d.
The reason you are seeing this is because of a rounding error.
Sin(PI()) is technically 0, as detailed in the Sin function documentation. However, excel returns 1.22515E-16 or 0.0000000000000001225148455
(i.e. approximately 0)
3.14159265358979 is a approximation of PI(), so it returns a different number that it also approximately 0. If you try 3.1415926535897, 3.141592653589 etc, you will get a different number each time.
This is likely related to working with floats, but I don't know enough about Excel and how it stores data to elaborate.
The SIN() function accepts the parameter in radians.
To convert an angle to radians, the value must be multiplied by PI()/180.
If you use =SIN(PI()), the PI is considered as a radian which is equal to RADIANS(180). Both =SIN(PI()) and =SIN(RADIANS(180)) returns the same result 0.
=SIN(3.14159265358979) is not actually equal to =SIN(PI()) because the PI in this context is different. And hence the different result.
The bottom line is to always use radians as a parameter for trigonometric functions.
While converting the hexadecimal value "FFFFFFFF00" into octal value using Hex2Oct of MS Excel, it should return "Error string" as per the rules mentioned here:
If number is negative, HEX2OCT ignores places and returns a 10-character octal number.
If number is negative, it cannot be less than FFE0000000, and if number is positive, it cannot be greater than 1FFFFFFF.
If number is not a valid hexadecimal number, HEX2OCT returns the #NUM! error value.
If HEX2OCT requires more than places characters, it returns the #NUM! error value.
If places is not an integer, it is truncated.
If places is nonnumeric, HEX2OCT returns the #VALUE! error value.
If places is negative, HEX2OCT returns the #NUM! error value.
But it computes and returns as "7777777400" without considering the rules/remarks mentioned in the link.
For example:
While calculating HEX2OCT,
As per Excel rule, If number is positive, it cannot be greater than 1FFFFFFF(hex)<->3777777777(oct)<->536870911(decimal).
But while calculating the HEX2OCT for FFFFFFFF00(hex) <-> 7777777400(oct) <-> 1099511627520(decimal).
Here the hex value FFFFFFFF00 is greater than 1FFFFFFF, but MS Excel does not return the error string instead it returns the converted octal value.
Can anyone explain why?
FFFFFFFF00 is actually well within the range of hex2oct because it is a negative number.
According to that documentation the largest negative number it can handle is FFE0000000 which when converted to decimal is -536870912. Converting your "big" hex over to decimal yields -256.
The reason the value of FFFFFFFF00 looks so big is because it's a negative number. The first bit is set to 1 (when converted to binary) which signifies that the number is negative. Negatives are computed in binary using two's complement which is found by flipping each bit and then adding 1 to the number.
Undoing the two's complement:
For your big number, the binary representation is:
1111111111111111111111111111111100000000
Subtracting 1:
1111111111111111111111111111111011111111
Flipping all the bits:
0000000000000000000000000000000100000000
Which is 256
So.. basically if the hex looks big, but the first bit is 1 then it's actually a small negative and well within your range of allowable values.
Lastly, when you hex2oct you don't get a negative sign for these because we are still not in decimal notation. The first bit of your octal is still a 1 (when converted to binary) since it's still the same number, just represented in a different counting system.
The clue lies earlier in the documentation page you quote:
The HEX2OCT function syntax has the following arguments:
Number Required. The hexadecimal number you want to convert. Number cannot contain more than 10 characters. The most significant
bit of number is the sign bit. The remaining 39 bits are magnitude
bits. Negative numbers are represented using two's-complement notation.
The hex value FFFFFFFF00 corresponds the binary value
1111 1111 1111 1111 1111 1111 1111 1111 0000 0000
and as the documentation says, "the most significant bit is the sign bit ... two's complement notation". So this value represents a negative number. By the rules of two's complement, it actually represents -256. And this is fine, because it is not "less than FFE0000000", as FFE0000000 is -2097152.
If you actually want to treat FFFFFFFF00 as an unsigned quantity, and get the octal representation of decimal 1099511627520, you'll need to use another method.
I've an input text field as below.
<h:inputText value="#{student[column.property]}" >
<f:validateLongRange minimum="1" maximum="#{marksdisplay.max_marks}" />
</h:inputText>
I'd like to validate if the number is from 1 to 100 and show error otherwise. How can I achieve this?
I know that most programming languages have functions built in for doing that for you, but how do those functions work?
The javadoc about the Double toString() method is quite comprehensive:
Creates a string representation of the double argument. All characters mentioned below are ASCII characters.
If the argument is NaN, the result is the string "NaN".
Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If the sign is negative, the first character of the result is '-' ('-'); if the sign is positive, no sign character appears in the result. As for the magnitude m:
If m is infinity, it is represented by the characters "Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-Infinity".
If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0".
If m is greater than or equal to 10^-3 but less than 10^7, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by '.' (.), followed by one or more decimal digits representing the fractional part of m.
If m is less than 10^-3 or not less than 10^7, then it is represented in so-called "computerized scientific notation." Let n be the unique integer such that 10^n<=m<10^(n+1); then let a be the mathematically exact quotient of m and 10^n so that 1<=a<10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by '.' (.), followed by decimal digits representing the fractional part of a, followed by the letter 'E' (E), followed by a representation of n as a decimal integer, as produced by the method Integer.toString(int).
How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.
Is that enough? Otherwise you might like to look up the implementation too...
A simple (but non-generic, naïve and slow way):
convert the number to an integer, then divide this value by 10 stepwise to find out its digits in reverse order. Concatenate them together and you have the integer representation.
substract the integer from the original number, now multiply by 10 stepwise and find the digits after the decimal point. Concatenate the first string with a point and this second string.
This has a few problems, of course:
slow as hell;
doesn't work for negative numbers;
won't give you exponential notation for very small or large numbers.
All in all, it's an idea, but not a very good one; I suspect there are no programming languages that do this.
This paper by Guy Steele provides details on how to do this correctly. It's much more subtle than you might think.
http://portal.acm.org/citation.cfm?id=93559
"Printing Floating-Point Numbers Quickly and Accurately" - Robert G. Burger
Scheme and C code for above.
As Oded mentioned in a comment, different languages will do this in different ways. As an example, here's how Ruby 1.9 does it (in C). Your best bet, just as a research exercise, will be to look into open-source languages and see how they do it.