Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why the math 1/2 dont return 0.5 ;

Why the math 1/2 dont return 0.5 ;

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 1.1k Views
  • 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.
  • X Offline
    X Offline
    xmaze
    wrote on 24 Jan 2015, 18:45 last edited by
    #1

    Hi, i have a strange problem, i have the code float test = 1/2;
    but returns test= 0 ;

    i cannot understand this, and my code don't work !
    See the screen shot from debugger!

    The problem is that the code on line 299 if(((dt%temp.period)==0), also dont work and i discover this inconvenience!

    !http://i57.tinypic.com/11r4oba.png!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on 24 Jan 2015, 19:00 last edited by
      #2

      Try
      @float test = 1.0/2.0; @

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xmaze
        wrote on 24 Jan 2015, 19:20 last edited by
        #3

        good that means in if state need to make a casting ?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ckakman
          wrote on 24 Jan 2015, 19:55 last edited by
          #4

          Yes, you need to cast one of them if both of then integers:
          @
          int a = 1;
          int b = 2;
          float test = float(a) / b;
          @

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 24 Jan 2015, 20:15 last edited by
            #5

            Not that any reasonable compiler wouldn't see this but why not just do this?
            @
            float test = 0.5f;
            @
            @andreyc - again, not that any compiler wouldn't see this but if you define a float avoid downcasting from a double:
            @
            float test = 1.0f/2.0f;
            @

            1 Reply Last reply
            0

            3/5

            24 Jan 2015, 19:20

            • Login

            • Login or register to search.
            3 out of 5
            • First post
              3/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved