How do I explain this inconsistency ?

0 favourites
  • 11 posts
  • Hi there ! <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy" />

    I'm no big connoisseur in trigonometry but I had to dip into it a little in order to generate a laser for my Run N' Gun shooter. I've managed to have my laser working but I've been puzzled by one small inconsistency I can't explain...

    (I've simplified the CapX and you can download it here :

    (to generate a laser you have to hold an arrow or more and use the left click)

    See, I have a debug box. When I add into my debug box Cos(270) it writes 0 as expected. Also Sin(270) writes -1.

    So that's good.

    But when I try to concatenate Cos(270)&" / "&Sin(270) the cosinus is wrong. It gives a figure veeeeery small but not nul. So it writes something like "-1.8369701987210297e-16 // -1" instead of "0 // -1".

    Is it because of the cosinus ?

    Or because of the concatenation ?

    More importantly, can this mess up my computation for the laser's width in any way ? (line 27-28 of the Event Sheet 1)

    Thanks in advance for taking the time to reassure me <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    Best regards

  • Why are you using strings for math?

  • What do you mean ?

    I can usually concatenate figures without problem

  • Well to start it's an extra step to convert strings into numbers, and then there's bound to be issues with the conversion, which could vary widely between browsers.

  • I'm converting a number into a string, sure, but that doesn't explain why Cos(270°) becomes this weird number when it's not alone. 0 converted into a string should still be "0", right ?

    Cos(270°) should always be strictly equal to zero... unless I don't understand cosinuses well (which is a possibility).

  • Could be 0, could be null.

    It depends on the browser, and how it's setup to handle it.

  • That's the problem with floating point arithmetic. There's nothing you can do about it, you just have to deal with it. However, a number like -1.8369701987210297e-16 is so small that you won't ever notice anything visually... so you might as well consider it to be 0.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yeah, the value is the real issue.

    Sorry I'm just off on the strings.

    That's like saying eggs+flour+milk+sugar=cookies

  • If you want to force the debug text to be more readable, I guess you could always do something like rounding the value to 5 or 6 decimals (or whatever number you deem significant).

    example for 5 decimals :

    Smaller values will then always be equal to 0.

  • Oh, it's just text.

    Well, nevermind all I said.

  • Ok, thanks for your explanation you two

    As long as it's not messing the calculations, it's ok. I'll use the rounding trick if I want a figure to appear in a debug log from now on.

    Cheers and thanks again

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)