Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. int casting does not behave with Math.Round with NaN?

int casting does not behave with Math.Round with NaN?

Scheduled Pinned Locked Moved C#
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Felix Cho
    wrote on last edited by
    #1

    Found a weird bug in an app that we are writing, and I narrowed down to this special case: double a = double.NaN; Console.WriteLine("a = {0}", a); Console.WriteLine("(int)a = {0}", (int)a); Console.WriteLine("Math.Round(a) = {0}", Math.Round(a)); Console.WriteLine("(int)Math.Round(a) = {0}", (int)Math.Round(a)); We just moved to 1.1 so this appears to be a 1.1 issue. For debug build, the output is: a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = 0 For release build, the output is: a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = -2147483648 It seemed for some reason Release does not behave the same and that was causing my problem. Granted I shouldn't depend on the cast to be 0 because it is NaN (should never depend on undefined behaviour ;P), but at least give me a consistent answer or throw an exception if NaN is used in Math.Round()? Again when my runtime was 1.0 it seemed fine, so could this be just a weird bug? :confused:

    F D 2 Replies Last reply
    0
    • F Felix Cho

      Found a weird bug in an app that we are writing, and I narrowed down to this special case: double a = double.NaN; Console.WriteLine("a = {0}", a); Console.WriteLine("(int)a = {0}", (int)a); Console.WriteLine("Math.Round(a) = {0}", Math.Round(a)); Console.WriteLine("(int)Math.Round(a) = {0}", (int)Math.Round(a)); We just moved to 1.1 so this appears to be a 1.1 issue. For debug build, the output is: a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = 0 For release build, the output is: a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = -2147483648 It seemed for some reason Release does not behave the same and that was causing my problem. Granted I shouldn't depend on the cast to be 0 because it is NaN (should never depend on undefined behaviour ;P), but at least give me a consistent answer or throw an exception if NaN is used in Math.Round()? Again when my runtime was 1.0 it seemed fine, so could this be just a weird bug? :confused:

      F Offline
      F Offline
      Felix Cho
      wrote on last edited by
      #2

      Correction: I have verified and this happens on both 1.0 and 1.1. Looks like an issue that no one has noticed before? ;)

      1 Reply Last reply
      0
      • F Felix Cho

        Found a weird bug in an app that we are writing, and I narrowed down to this special case: double a = double.NaN; Console.WriteLine("a = {0}", a); Console.WriteLine("(int)a = {0}", (int)a); Console.WriteLine("Math.Round(a) = {0}", Math.Round(a)); Console.WriteLine("(int)Math.Round(a) = {0}", (int)Math.Round(a)); We just moved to 1.1 so this appears to be a 1.1 issue. For debug build, the output is: a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = 0 For release build, the output is: a = NaN (int)a = 0 Math.Round(a) = NaN (int)Math.Round(a) = -2147483648 It seemed for some reason Release does not behave the same and that was causing my problem. Granted I shouldn't depend on the cast to be 0 because it is NaN (should never depend on undefined behaviour ;P), but at least give me a consistent answer or throw an exception if NaN is used in Math.Round()? Again when my runtime was 1.0 it seemed fine, so could this be just a weird bug? :confused:

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Since Nan is not a real number, I wouldn't expect int casting to work predictably anyway. The easiest way around it is to check if a IsNaN before attempting the Round and int cast operations. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups