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 abs wont work on neg val
Forum Updated to NodeBB v4.3 + New Features

Why abs wont work on neg val

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 312 Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    If doing

    qint64 n=std::numeric_limits<qint64>::min();
    n=qAbs(n);
    
    if(n<0)
    It returns true...
    
    But if adding just 1 number its ok.
    
    Why is that?
    
    JKSHJ 1 Reply Last reply
    0
    • ? A Former User

      If doing

      qint64 n=std::numeric_limits<qint64>::min();
      n=qAbs(n);
      
      if(n<0)
      It returns true...
      
      But if adding just 1 number its ok.
      
      Why is that?
      
      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Q139 said in Why abs wont work on neg val:

      Why is that?

      Because the absolute value of std::numeric_limits<qint64>::min() is greater than std::numeric_limits<qint64>::max().

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        See also https://en.cppreference.com/w/cpp/numeric/math/abs :

        The behavior is undefined if the result cannot be represented by the return type.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2

        • Login

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