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. Proplem toDouble QString?
Forum Updated to NodeBB v4.3 + New Features

Proplem toDouble QString?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.1k Views 3 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.
  • H Offline
    H Offline
    HAHAHAHAHA
    wrote on last edited by
    #1

    I have code:

    QString number = "1.001";
    double value = number.toDouble();
    double value1 = 1;
    if(value - value1 < 0.001)
    {
        qDebug()<<"True";
    }else
    {
      qDebug()<<"False";
    }
    

    I run and show log "True". I do not understand why this is so. Please help me!

    ValentinMicheletV 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi. double is a floating point number. It can not represent 0.001 exactly. It's more like 0.000999999999999889865875...

      1 Reply Last reply
      0
      • H HAHAHAHAHA

        I have code:

        QString number = "1.001";
        double value = number.toDouble();
        double value1 = 1;
        if(value - value1 < 0.001)
        {
            qDebug()<<"True";
        }else
        {
          qDebug()<<"False";
        }
        

        I run and show log "True". I do not understand why this is so. Please help me!

        ValentinMicheletV Offline
        ValentinMicheletV Offline
        ValentinMichelet
        wrote on last edited by ValentinMichelet
        #3

        @HAHAHAHAHA
        This is not a problem with Qt. Using double always leads to precision errors.

        double value = 1.001;
        double value1 = 1.000;
        if (value - value1 < 0.001)
        {
          qDebug()<<"True";
        }
        

        This will print True

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          just in case you are wondering why this is like it is, here is a detailed explaination.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          kshegunovK 1 Reply Last reply
          2
          • raven-worxR raven-worx

            just in case you are wondering why this is like it is, here is a detailed explaination.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @raven-worx
            Superb resource. You'd probably be surprised how many non-computer scientists and regular programmers haven't the slightest idea of those issues.

            @HAHAHAHAHA
            If you wish to compare two floating points you could use Qt's qFuzzyCompare function or qFuzzyIsNull to check if a floating point number is zero.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • H Offline
              H Offline
              HAHAHAHAHA
              wrote on last edited by
              #6

              I understand. Thank all =))

              1 Reply Last reply
              0

              • Login

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