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. Problem to detect minimum value of a negative number
Forum Updated to NodeBB v4.3 + New Features

Problem to detect minimum value of a negative number

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 885 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.
  • P Offline
    P Offline
    pedromenezes
    wrote on 10 Feb 2019, 19:20 last edited by
    #1

    Hi guys,

    I'm trying to detect the maximum and the minimum y value from a plot. The maximum is ok. But I can not detect the correct negative number. I guess the problem may be the variable type, however I could not solve.

    My code is:

       auto plot10 = ui->customPlot_10;
       double y_max = 0.0;
       double y_min = 0.0;
    
       QVector<double> x(256), y(256);
    
           for (int i=0; i<x.size(); ++i)
           {
           x[i] = plot10->graph(0)->data()->at(i)->key;
           y[i] = plot10->graph(0)->data()->at(i)->value;  
                 
           if (y[i]>=y_max) y_max = y[i];
           if (y[i]<=y_min) y_min = y[i];
           qDebug () << "Min i: " << y_min;
           qDebug () << "Max i: " << y_max;
    
           }
    

    The Debug result shows that the code ignore the number signal, as you can see:

    Min i:  -0.14
    Min i:  -0.14
    Min i:  -0.14
    Min i:  -0.14
    Min i:  0.18
    Min i:  0.22
    Min i:  0.26
    Min i:  0.28
    Min i:  0.31
    Min i:  0.33
    Min i:  0.34
    //...
    

    Could someone help me?

    Cheers,

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 10 Feb 2019, 19:28 last edited by mrjj 2 Oct 2019, 19:32
      #2

      Hi
      I would start by doing

      qDebug () << "value: " << y[i];
      to see the input data.

      You are using doubles so it should not be the issues since its mostly doing ==
      (is equal that can be tricky )

      That said
      You could use
      #include <algorithm>
      double min = *std::min_element(vec.constBegin(), vec.constEnd());
      double max = *std::max_element(vec.constBegin(), vec.constEnd());
      If you build the vector first.

      1 Reply Last reply
      5
      • K Offline
        K Offline
        kenchan
        wrote on 10 Feb 2019, 23:47 last edited by kenchan 2 Oct 2019, 23:48
        #3

        HI, I don't think this will work with both <= and >= conditions. They should be just < or > and you should init them with y_max as a smaller value than the range you expect and y_min with a larger value than the range you expect.

        y_max = REAL_MIN; // or something else very small
        y_min = REAL_MAX; // or something else very large
        ...
        if (y[i]>y_max) y_max = y[i];
        if (y[i]<y_min) y_min = y[i];
        

        should work better.

        1 Reply Last reply
        2
        • V Offline
          V Offline
          VRonin
          wrote on 11 Feb 2019, 08:43 last edited by
          #4

          Don't reinvent the wheel

          QSharedPointer<QCPGraphDataContainer > graphData = ui->customPlot_10->graph(0)->data();
          const double y_max = std::max_element(graphData->cbegin(),graphData->cend(),[](const QCPGraphData& a, const QCPGraphData& b)->bool{return a.value<b.value;})->value;
          const double y_min = std::min_element(graphData->cbegin(),graphData->cend(),[](const QCPGraphData& a, const QCPGraphData& b)->bool{return a.value<b.value;})->value;
          const double x_max = std::max_element(graphData->cbegin(),graphData->cend(),[](const QCPGraphData& a, const QCPGraphData& b)->bool{return a.key<b.key;})->key;
          const double x_min = std::min_element(graphData->cbegin(),graphData->cend(),[](const QCPGraphData& a, const QCPGraphData& b)->bool{return a.key<b.key;})->key;
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          5
          • P Offline
            P Offline
            pedromenezes
            wrote on 13 Feb 2019, 16:42 last edited by
            #5

            Thank you guys your solutions are great and i'm gonna to use in different applications!!

            Cheers,

            1 Reply Last reply
            0

            1/5

            10 Feb 2019, 19:20

            • Login

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