2
$\begingroup$

If I write this:

function f(n: Int) : Double {
    return 1.5*n;
}

I get an error:

The arguments to the binary operator do not have a common base type.

Apparently I need a function to turn n from an Int into a Double. There are lots of functions going from double to int, like Microsoft.Quantum.Extensions.Math.Floor, but I wasn't able to find anything from int to double.

$\endgroup$
5
  • $\begingroup$ You mean you want to know how subtyping is indicated in this language. Because (Num a) => a -> a -> a is not the same as (Num a,Num b) => a -> b -> a as Haskell would denote it. $\endgroup$
    – AHusain
    Dec 30, 2018 at 4:16
  • $\begingroup$ @AHusain No, I was really just looking for the ToDouble function and couldn't find it. I know it's a dumb question, but that's the point of stack exchange: make task-oriented and problem-oriented documentation that's faster than searching the manual. $\endgroup$ Dec 30, 2018 at 4:34
  • $\begingroup$ This sort of question would be all good from someone who was new and didn't have the terminology to search. But it just doesn't seem to make sense to me coming from someone who already knows what to search for and how. $\endgroup$
    – AHusain
    Dec 30, 2018 at 5:09
  • 1
    $\begingroup$ @AHusain I actually didn't know what to search for, or how. The documentation on types didn't say anything about it, autocomplete wasn't working, google doesn't understand that Q# is a term, and I didn't realize there was supposed to be a function list on the docs site (I use noscript and it just doesn't show unless you allow scripts). $\endgroup$ Dec 30, 2018 at 8:54
  • 3
    $\begingroup$ @CraigGidney: One thing we've found useful in searching for Q# on Google is to use "qsharp" as a search term rather than Q#. The query "qsharp" convert int to double returns ToDouble as the second hit for me in a private tab. $\endgroup$ Dec 31, 2018 at 5:21

1 Answer 1

5
$\begingroup$

You want the Microsoft.Quantum.Extensions.Convert.ToDouble function (deprecated in favor of Microsoft.Quantum.Convert.IntAsDouble in 0.6 release).

open Microsoft.Quantum.Extensions.Convert;

function f(n: Int) : Double {
    return 1.5*ToDouble(n);
}

The reason it works this way is because in Q# (Num a) => a -> a -> a is not the same as (Num a,Num b) => a -> b -> a as Haskell would denote it.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.