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. .NET (Core and Framework)
  4. Type conversion strange bug

Type conversion strange bug

Scheduled Pinned Locked Moved .NET (Core and Framework)
help
3 Posts 3 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.
  • C Offline
    C Offline
    cybertone
    wrote on last edited by
    #1

    Yesterday I've caught a very strange bug float a = 81.1666641F; float b = 18F; float c = 6.3166666F; float d = (a - b) / c; int e = (int)((a - b) / c); int f = (int)d; Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); -------------------------- OUTPUT: 10.0 9 10 :~

    L J 2 Replies Last reply
    0
    • C cybertone

      Yesterday I've caught a very strange bug float a = 81.1666641F; float b = 18F; float c = 6.3166666F; float d = (a - b) / c; int e = (int)((a - b) / c); int f = (int)d; Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); -------------------------- OUTPUT: 10.0 9 10 :~

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, there is no bug here. It is a result of rounding (and the implicit conversion rules). Do the same code with double, and try to explain ! :)

      Luc Pattyn [My Articles]

      1 Reply Last reply
      0
      • C cybertone

        Yesterday I've caught a very strange bug float a = 81.1666641F; float b = 18F; float c = 6.3166666F; float d = (a - b) / c; int e = (int)((a - b) / c); int f = (int)d; Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); -------------------------- OUTPUT: 10.0 9 10 :~

        J Offline
        J Offline
        Jaiprakash M Bankolli
        wrote on last edited by
        #3

        it is not a bug you are casting the result set to in so you are getting the results in Int which is close round of result

        Regards, Jaiprakash M Bankolli jaiprakash.bankolli@gmail.com http://jaiprakash.blog.com/

        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