Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug C# compiler redundant cast bug?

Discussion in '2019.3 Beta' started by Quatum1000, Jan 17, 2020.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Hi,

    Code (CSharp):
    1. public double StepSize =0d;
    2. public int MeterPerTextureTile = 200;
    3. public int _TextureRes = 256;
    4. ...
    5. StepSize = MeterPerTextureTile / _TextureRes;
    StepSize = 0.0 wrong but expectable result. But the result is infinity.

    Using
    Code (CSharp):
    1. StepSize = (double)MeterPerTextureTile / (double)_TextureRes;
    It's for sure correct.

    But the c# phraser display an redundant cast.

    Untitled-1.jpg




    Believing the c# phraser
    1. StepSize = MeterPerTextureTile / _TextureRes;
    ... results in some very ugly { NaN, NaN, NaN } in later calculations and these are extremely difficultly to uncover.
     
    Last edited: Jan 17, 2020
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    One of the casts is redundant. The other one is not. You need to cast one of the operands to double, and then the whole division will be treated in "doubles".
     
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Okay, but clicking fix removes both. As you can see both are grey'ed.
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,799
    But isnt that a VS issue rather than a unity one?
     
    elcionap, QFSW, Zipe92 and 1 other person like this.
  5. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    And nothing to do with the beta at all, unless this behaviour is inconsistent with a previous version
     
  6. Awarisu

    Awarisu

    Joined:
    May 6, 2019
    Posts:
    215
    This is not an issue with Unity (you can get the same in a simple C# console project), but even if you only wanted to highlight one of them, which one is truly redundant? (neither)

    If you clicked on the light bulb to "fix" the problem, I just verified that VS correctly only removes one double cast, and the other changes to not be highlighted as redundant. You can pick whichever you want, my personal preference is having the cast be as early as possible so you can clearly see this is doing floating point.