# Wrong time parameter in wait statement



## Blackster (Feb 19, 2013)

Hi all,

I need to consult you guys again because of script warning I can't resolve! Like the subject says it gives me the following message: "SCRIPT WARNING: wrong time parameter in wait statement!"

I have figured out that it is the following line which causes the error:

wait($a/100*$DURATION_SIXTEENTH*16)

What I'm trying to do here is to shorten a given note length (a whole note in my case). $a is an integer variable with a range of 1 to 99. 

I assume that Kontakt can't read the result of this equation as it might not be in the right format or something. Is it even possible to manipulate in-built $DURATION-variables like I do here? I refer to "$a/100", not to "*16" ...

Mmmhh. Many thanks in advance!


----------



## Sasje (Feb 19, 2013)

I have no problem with your script? works as expected like this:



```
on init
   declare $duration
   declare $a := 100
   declare $t
end on

on note
   $duration := $DURATION_SIXTEENTH
   $t := $a / 100 * $duration * 16
   message($t & " Duration:" & $duration)
   wait($t)
end on
```


----------



## Blackster (Feb 19, 2013)

Hi Sasje,

thanks for checking this quickly. Your report is a bid sign for me because that means the mistake is hidden somewhere else .... :?


----------



## Blackster (Feb 19, 2013)

@Sasje: I've tested the script and it does not seem to work properly. If you change $a to a number smaller than 100 $t is set to 0.

So, I assume, there IS something wrong with that line ...


----------



## Blackster (Feb 19, 2013)

If you change the line of $t to the following, it works ...

$t := $duration * 16 * $a / 100

That is really weired since it's the same equation. But I assume the Kontakt is not able to handle $a/100 at the beginning of the line because it will result in a floating point number smaller than 1 .... at least that is what I'm guessing now ... 

I'll try that with my own script and will report here.


----------



## TBAudio (Feb 19, 2013)

Blackster @ Tue Feb 19 said:


> . But I assume the Kontakt is not able to handle $a/100 at the beginning of the line because it will result in a floating point number smaller than 1 .... at least that is what I'm guessing now ... .


You are correct that is causing the error, which is due to the wait time being 0. For a quick fix, what you could do is put an if statement in to only run the wait scenario if a is larger than or equal to 100.


```
on note
   $duration := $DURATION_SIXTEENTH
   if ($a >= 100)
       $t := $a / 100 * $duration * 16
       message($t & " Duration:" & $duration)
       wait($t)
    end if
end on
```

edit - I see however in your OP that $a is only 1-99. of course not till after I posted as usual. I think in this case you just need to change the order of operation so that $a/100 never returns 0. you could also multiply up and divide back down which is a common solution to floating point problems in ksp.

($a * $duration * 16) / 100 ....try this line with your original script instead, forget my entire post about the if statement. 

I'm sure there are a few ways to solve this one.


----------



## Big Bob (Feb 19, 2013)

Because of the all-integer arithmetic, when you have a chained expression involving multiplies and divides, you should always do as many of the mulitplies (that you can without overflow) *before* you do any divisions.

While mathematically x := a/100*250 is equal to a*250/100, in the KSP's all-integer world, these two will not produce equal results. If a < 100, the first expression will always evaluate to zero. Whereas the 2nd expression will evaluate correctly, at least to the closest 'floor' integer. If you want it to evaluate to the closest integer, you need to round the calculation like this:

(a*250 + 50)/100

Rejoice,

Bob

BTW I realize the example above that I'm using is not specifically your expression but I'm trying to keep it simple so as not to obscure the point I'm trying to make. I should also mention that if 'a' can be both positive or negative, then the rounding technique has to be more sophisticated. But, as long as 'a' is always zero or positive, the above technique will work.


----------



## Blackster (Feb 19, 2013)

Thank you all! Yes, I see the problem very clearly now so the solution is quite easy  ... these are the times I wished KSP was more like C ...


----------

