0

Does anybody know if there is any power function added for SPARQL?

My quick-fix is using BIND and string process but it's not optimal at all:

?contract part:price ?price_E ;
    part:Currency ?currency .

BIND(strafter(str(?price_E), "E") as ?E)
BIND(replace(strbefore(str(?price_E), "E"), "\[.\]", "") as ?nodot)
BIND(substr(?nodot, 0, xsd:integer(?E)+2) as ?first)
BIND(substr(?nodot, xsd:integer(?E)+2, 2) as ?second)
BIND(concat(?first, ".", ?second) as ?price)

Example of input and output data: Input 1.51535E3 Output 1515.35

Ckk
  • 3
  • 2
  • Maybe you can explain why you need to do that. This reference: https://www.w3.org/TR/sparql11-query/#rDOUBLE seems to suggest that `mmmEnnn` and `mmmennn` are recognized as floating point numbers; it seems like you don't have to do that parsing. I think it would help others understand if you explain what else is happening here that makes it necessary. – Robert Dodier Feb 10 '23 at 18:46
  • I also don't get the purpose of this conversion, rendering happens on the client side, handling and formatting scientific notation should be capable by any programming language. – UninformedUser Feb 11 '23 at 06:21
  • That said, you could always convert it to `xsd:decimal` which doesn't have the scientific notation: `BIND(xsd:decimal(?price_E) AS ?price_E_decimal)` - indeed, for larger numbers this conversion might fail, as 32bit vs 64bit. But I assume for prices this would never be an issue – UninformedUser Feb 11 '23 at 06:21
  • 1
    Casting decimal to double is safe up to 15 significant decimal digits. A 32 bit number 10 digits. An unsigned 64 bit number is 20 digits. – AndyS Feb 11 '23 at 18:49
  • Somehow I didn't think of converting it to xsd:decimal, just tried xsd:float (*facepalm*).. Thank you for your comments! :) – Ckk Feb 15 '23 at 10:43

1 Answers1

1

Sadly, the answer is no. As of SPARQL 1.1, the current W3C version, there is no exponentiation operator. You can accomplish it with repeated multiplication (x*x) as in:

(COUNT(?items)*COUNT(?items)

However, in your example, it is not clear that you actually need to do any arithmetic. The input 1.51535E3 is in fact already equal to 1515.35 because it is valid scientific notation, which SPARQL supports.

John Skiles Skinner
  • 1,611
  • 1
  • 8
  • 21