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 / C++ / MFC
  4. C++ Math-Failure? modfl == mudfl?

C++ Math-Failure? modfl == mudfl?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpquestionc++visual-studiohardware
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
    ClockDivider
    wrote on last edited by
    #1

    Hello, I wonder about the behaviour of the modfl-function that splits a double into it´s integer and fractional part, and beside this why double values seem to have different values in different C++ IDEs. Here is my code: long double dVal; // Value to split into fractional and integer part long double dInt; // takes the integer part long double dFract; // takes the fractional part 1. dVal = 3333.3333; dFract = modfl(dVal,&dInt); //Result: // dInt = 3333.0000000000 // dFract = 0.33329999999978 2. dVal *= 10000; // should be 33333333.0 now? dFract = modfl(dVal,&dInt); // Result: // dInt = 33333332.0000000000 // dFract = 0.99999999627471 3. dVal = 33333333.0; dFract = modfl(dVal,&dInt); //Result: // dInt = 33333333.0000000000 // dFract = 0.0000000000 In Example 1, the dVal is stated to be 3333.3332999999998 when looking at it with the Visual Studio .NET debugger (watch), in Embedded C++ 4.0 it is 3333.33330000000, this is directly after assigning the constant value. Why is this? And especially: why does only the last example come up with correct values? Thanks... Martin

    M A 2 Replies Last reply
    0
    • C ClockDivider

      Hello, I wonder about the behaviour of the modfl-function that splits a double into it´s integer and fractional part, and beside this why double values seem to have different values in different C++ IDEs. Here is my code: long double dVal; // Value to split into fractional and integer part long double dInt; // takes the integer part long double dFract; // takes the fractional part 1. dVal = 3333.3333; dFract = modfl(dVal,&dInt); //Result: // dInt = 3333.0000000000 // dFract = 0.33329999999978 2. dVal *= 10000; // should be 33333333.0 now? dFract = modfl(dVal,&dInt); // Result: // dInt = 33333332.0000000000 // dFract = 0.99999999627471 3. dVal = 33333333.0; dFract = modfl(dVal,&dInt); //Result: // dInt = 33333333.0000000000 // dFract = 0.0000000000 In Example 1, the dVal is stated to be 3333.3332999999998 when looking at it with the Visual Studio .NET debugger (watch), in Embedded C++ 4.0 it is 3333.33330000000, this is directly after assigning the constant value. Why is this? And especially: why does only the last example come up with correct values? Thanks... Martin

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      this is normal floating point behaviour when applied to computers.


      Maximilien Lincourt Your Head A Splode - Strong Bad

      1 Reply Last reply
      0
      • C ClockDivider

        Hello, I wonder about the behaviour of the modfl-function that splits a double into it´s integer and fractional part, and beside this why double values seem to have different values in different C++ IDEs. Here is my code: long double dVal; // Value to split into fractional and integer part long double dInt; // takes the integer part long double dFract; // takes the fractional part 1. dVal = 3333.3333; dFract = modfl(dVal,&dInt); //Result: // dInt = 3333.0000000000 // dFract = 0.33329999999978 2. dVal *= 10000; // should be 33333333.0 now? dFract = modfl(dVal,&dInt); // Result: // dInt = 33333332.0000000000 // dFract = 0.99999999627471 3. dVal = 33333333.0; dFract = modfl(dVal,&dInt); //Result: // dInt = 33333333.0000000000 // dFract = 0.0000000000 In Example 1, the dVal is stated to be 3333.3332999999998 when looking at it with the Visual Studio .NET debugger (watch), in Embedded C++ 4.0 it is 3333.33330000000, this is directly after assigning the constant value. Why is this? And especially: why does only the last example come up with correct values? Thanks... Martin

        A Offline
        A Offline
        Alex_Y
        wrote on last edited by
        #3

        This how double works. if you assign var to 2.0 it will be stored as 2.0 but when it will start use it will be presented as 1.999999999(9) :)

        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