I'm learning about XML and XPath in my university course (XPath 1.0 to be exact) and I stumbled upon something. F.e. if I write an expression:
NaN < true()
it returns true. Which is very weird. When I do.
NaN < 1
it returns false. AFAIK XPath 1.0 comparisons (without node-sets) first convert the values to numbers and only then compares them. So, in the first expression it goes like this
NaN < number(true()) -> NaN < 1
and in the second
NaN < 1
I'd like to get to the bottom of this, so any information would be helpful. I didn't find anything concerning this situation in the docs. Maybe I missed something.
Thanks in advance!
The token NaN does not represent the double value NaN, it is short for child::NaN which returns an empty node-set (unless of course your XML document contains an element named NaN).
I'm a little surprised that a university should be teaching XPath 1.0 which has long been superseded by new versions. Of course lots of people are still using it, but I would have thought a university would teach a newer version.
Related
I'm new to Mathematica and I am struggling with the following problem:
If I evaluate the expression N[CDF[NormalDistribution[m,s],x]] for m=12, s=25 and x=10, i get 0.468119 (=the value I would expect), however, if I evaluate the expression CDF[NormalDistribution[m,s],x] for the same x,m,s, i get 0.324195.
I checked the documentation for Mathematica and I cannot spot the difference except that N[..] returns the numerical value of the expression inside the brackets.
Any ideas? Perhaps its soo simple but i don't see it..
Thank you
I'm working on a validator that validates turtle files. When working on a function to check if the cardinality that's stated is correct for each object, I can't figure out how to access the value of a literal.
The literal value is Card=literal(type(xsd:nonNegativeInteger, '1')) (or 1^^'http://www.w3.org/2001/XMLSchema#nonNegativeInteger').
I find a bag of properties of length L. How can I check that L == Card?
I already tried the following:
% L and Card are both 1
rdf_canonical_literal(L, LiteralL), rdf_compare(=, LiteralL, Card).
% false
rdf_canonical_literal(L, LiteralL).
% LiteralL = 1^^'http://www.w3.org/2001/XMLSchema#integer'.
The problem is that xsd:integer and xsd:nonNegativeInteger don't compare as equal.
However, the easiest thing to me would seem to get the value of Card but I really don't see how to do it. Any solutions or pointers where to find an example of this would be much appreciated!!
If you use library rdf11 then most common datatype IRIs are automatically interpreted as Prolog values. In other words: there is no need to convert from RDF literals to Prolog values at all. Example:
?- [library(semweb/rdf11)].
?- rdf_assert(rdf:a, rdf:b, 1^^xsd:int).
?- rdf(_S, _P, N^^xsd:int).
N = 1.
You can extend library rdf11 with a hook for less common datatype IRIs, e.g., I use a lot of geographic data (datatype IRI geo:wktLiteral) which I let rdf/[3,4] interpret as Prolog Well-Known Text (WKT) notation automatically.
I'm trying to make a "complex calculation" in a freemarker template. The calculation calls for a modulus call. At first I kept simplifying my code, and finally just hardcoding values to try and figure out the cause, but it seems that freemarker's mod function ALWAYS returns int values?? but they're not even rounded, they're truncated. I need them rounded (.5 up would work). My most simplified code that does NOT work is this:
<#assign p_year_m1 = (2503.638 % 7.00)?float>
the longer version is:
<#assign p_year_m1 = ((bdy_m1 + (bdy_m1/4.0)-(bdy_m1/100.0)+(bdy_m1/400.0)))%7>
So the correct answer should be 4.638, rounds up to 5, but I'm getting 4.00 even if I wrap ?string[0.00] around it.
What am I missing? Surely there has to be a way to make this work! If not, I guess I'd need help constructing a work around to get similar results.
Thanks.
PS - the var, p_year_m1 is not intialized before this, so I believe if I try to store a float in there it should hold a float.
This is the calculation I'm trying to replicate
I can confirm that % gives the remainder of integer division. It casts (truncates) both arguments to integers, and then calculates the remainder. It's weird for sure, but can't be changed because of backward compatibility. The main application is calculating "zebra tables" and such, where it's might as well desirable, I don't know... it had just get in like that in the early times of the project, and then stuck.
I am having real difficulty with this and every answer I have seen doesnt seem to work. I have been able to pass a value such as 1.44 as 1.00 but the two decimal values are being lost. I have a number of values passed from a from which i then want to submit to an api via a call. The code is below:
IncomeWagesWeekly = params[:WagesWeekly].to_i
How do i ensure that when this is passed the two decimal places are present. Thanks for any help.
You don't have in Ruby language such thing as fixed digits after decimal point.
1 is the same as 1.00 as it was rightfully mentioned before in comment (almost the same).
If you don't check it's type like that:
1.is_a? Integer # => true
1.is_a? Float # => false
it is all the same.
Just use 1.44.to_i.
If by some reason you want to have an instance of Float instead use to_f method. To crop number explicitly you should use round, or ceil, or floor method:
1.44.round.to_f # => 1.0
1.55.round.to_f # => 2.0
1.44.ceil.to_f # => 2.0
1.55.ceil.to_f # => 2.0
1.44.floor.to_f # => 1.0
1.55.floor.to_f # => 1.0
Maybe don't try to make in integer and use to_f instead. Also consider any sprintf( "%0.02f", your_number), but it returns string.
First, it seems that you want to do fixed-point arithmetic here. So, using a floating point number is not the right choice, since floating point calculations can produce mathematically incorrect results.
A solution for this would be either to stick with Integers (as has been suggested already), or to use the data type BigDecimal, which is defined in the Ruby standard library, and in particular its methods fix, frac and to_digits.
Now to your database: You didn't say what database you are using, and how you pass the values to it, but in general, it is a bad idea to store a non-integer value into a database-field which is supposed to accept integers. As you observed, the fractional part was dropped. Correct behaviour.
You could redefine your database schema, or you can convert your decimal value by yourself into something which matches the field definition in the database. Which way to go, depends on what you want to actually do with this value afterwards. For instance, if you just want to display it, but not perform any calculations, you could use a string. Or, if you know that the number of fractional digits don't exceed a certain limit, you could define a suitable numeric format for the database column - etc.
The following piece of code works perfectly in script/console but returns the following error when i compile the same in a ruby script.:
:in `round': wrong number of arguments (1 for 0) (ArgumentError)
tf={"ph"=>{0=>1.33333333333333, 1=>1.5}, "fee"=>{0=>1.66666666666667}, "test"=>{0=>1.16666666666667, 1=>1.25}, "what"=>{0=>2.0, 1=>2.0}, "for"=>{0=>1.5}, "is"=>{0=>1.83333333333333, 1=>1.75}}
tf.each{|k,v| v.each{|k1,v1| tf[k][k1]=(v1.round(5))}}
Any Ideas ? Cheers !
Float#round seems to work differently in Ruby 1.8 and Ruby 1.9: in 1.8 it complains about the given argument, in 1.9 returns back float properly rounded to the given number of decimals.
But, as the article linked in the other answer wisely says:
you should consider the reason you’re
performing the rounding (or
equivalent) operation. If it’s for
presentation reasons only a better way
might be to use a format string
instead, and leave the original data
intact.
From what it looks like, you are not supposed to pass an argument to the round method. You have passed 5 to it.
If you are trying to round it to 5 decimal places, there is no builtin method for that (that I'm aware of). This is a page that explains how to do so: http://solutions.hans-eric.com/rounding-off-floating-point-numbers-in-ruby