Random in DrawScript
David R. Lane (77) 766 posts |
In BASIC RND/10 gives me what I expect: One of the ten numbers from 0.1 to 1 inclusive with one decimal place. The same function in DrawScript often doesn’t give these exactly, for example, it has spewed out 0.899999999 instead of 0.9 or 0.600000002 instead of 0.6. How come? Added: The formula isn’t being represented correctly in textile. To spell it out it is |
Jeffrey Lee (213) 6048 posts |
Welcome to the world of floating point rounding errors! 1/10 can’t be expressed accurately in binary; it’s the infinitely recurring sequence 0.0001100110011001100110011… So when you ask for RND(10)/10 you really do get numbers like 0.899999999 or 0.600000002 as a result. Depending on how many digits of precision are used when displaying the number (and on how accurate the binary to decimal conversion is!), you’ll either see the inaccuracy or it will get rounded to the expected (but technically incorrect) value like 0.9 or 0.6. (PS – you can use <notextile> … </notextile> to (mostly?) stop textile messing with text) |
David R. Lane (77) 766 posts |
Yes, thanks for the explanation, I thought it was something like that. I have got round the problem by doing everything with integer variables. Added: Eh! What a joke, I have used the “notextile” tags as you suggest and the inequality has come out twice immediately before me saying “but without repetitions”. :-(( |
Rick Murray (539) 13851 posts |
Welcome to Textile. :-) |