0 votes
by (200 points)

I can't get the sqrt or pow macros to work.

For example

(set: $num to (sqrt: 25) )

Just stores a 0 in $num. No matter what I give it as input, sqrt seems to evaluate to zero.

Likewise, even though the documentation says pow should be used like (pow: 5, 2), it complains that pow only takes 1 argument. Giving it 1 argument evaluates to zero. 

I've tried testing in a few different browsers and with the online and downloadable version of Twine. 

I'm not sure what I am doing wrong with these functions. Any help is appreciated.

1 Answer

+1 vote
by (159k points)
selected by
 
Best answer

You aren't doing anything wrong, those macros are currently broken.

NOTE:
strongly suggest you create an New Issue about this problem on the Harlowe project's repository so that the developer can fix it, if you don't then it is very likely that the developer won't know that the issue exists. Developers can't fix things if no-one tells them about the bugs, and the main reason I'm not creating the issue for you is that it is better if they come from the people who actually had the problem because you're better able to supply more details if asked that I would be third-hand.

In the short term while waiting for the bug to be fixed you can use the Javascript Math object's methods to do the same functionality, which is what those macros are using internally.

(set: $num to Math.sqrt(25))\
sqrt: $num

(set: $num to Math.pow(2, 8))\
pow: $num

 

by (68.6k points)

Merely as an FYI and not to take away from greyelf's excellent answer.  Note that the proposed workaround only works with number literals.  Because of the way Harlowe works, you cannot currently pass $variables into functions and methods.

For example:

<!-- As shown by greyelf, this works. -->
(set: $a to Math.sqrt(25))sqrt of 25: $a

<!-- This, however, does not. -->
(set: $n to 25)\
(set: $b to Math.sqrt($n))sqrt of $n: $b

 

by (200 points)

Thank you for that comment, it saved me a lot of additional time. 

I did find a way to pass variables to functions. You are right about not being able to pass variables, normally, but it seems it has less to do with them being literals and more of a parsing issue with the $. As long as the $ does not come first in the function, it seems to work:

<!--This does not work, as mentioned-->
(set: $n to 25)\
(set: $b to Math.sqrt($n))
sqrt of $n: $b

<!--This seems to work-->
(set: $num to 25)
(set: $num2 to Math.sqrt( 0 + $num ) )

<!--This also seems to work-->
(set: $num to 8)
(set: $num2 to Math.pow(2,$num) )

<!-- But this does not work -->

(set: $num to 2)
(set: $num2 to Math.pow($num2,8) )

I feel stupid having to write that, but it works, and shouldn't break if the bug is eventually fixed, so I guess I am going with it.

...