Frage

I have the following Jena Rule to perform addition:

@prefix pm:     <http://example.org/ProductionMesure#>.
@prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix owl:    <http://www.w3.org/2002/07/owl#>.
@prefix pr:     <http://example.org/Production#>.

[r1:
(?mesCavTur rdf:type        pr:Mesure)
(?cavTur    owl:ensemble_de ?mesCavTur)
noValue(?cavTur owl:valeur ?x)
-> 
(?cavTur    owl:valeur      0.0)
]

[r2:
(?mesCavTur rdf:type        pr:Mesure)
(?mesCavTur owl:valeur      ?valeur)
noValue(?mesCavTur pm:flag pm:done)
(?cavTur    owl:ensemble_de ?mesCavTur)
(?cavTur    owl:valeur      ?oldValeur)
sum(?valeur, ?oldValeur, ?somme)
-> 
drop(4)
(?cavTur    owl:valeur      ?somme)
(?mesCavTur pm:flag pm:done)
hide(pm:flag)
]

The rule is working almost correctly to perform the desired addition result. The problem which I have is that the values that are being passed into the sum function are both floats, but the returned value (?somme) comes back as a double.

The unexpected result from this is that it adds a small decimal number to the result (which I assume has something to do with the type conversion).

For example if I want to find the sum of three values 2.2, 2.5, and 2.7; the result I obtain is 7.1000000953674316 (which is close but not quite right).

Is there a way using this Jena rule, that I might be able to convert my float values (?valeur & ?oldValeur) into type double, then perform the sum function with these new double values, then finally convert the output (?somme) into a float value?

I think by doing this the extra decimal places may not be added to my output.

War es hilfreich?

Lösung

Rounding problems are common when display floating point numbers, as most floating point numbers do not have a precise representation in binary. So even if you were to use floats instead of doubles, the problem of small approximation errors would still arise.

You have two choices. You can work with integers only, for example by scaling (multiplying) all of your numbers by 100 or 1000, and then remembering to scale them back when you produce output for your users. Or, you can treat the display problem as a separate issue, and when you come to render numbers in your output, use a number formatting library, such as the one built in to Java, or other suggestions on StackOverflow. Personally, I would make the second choice.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top