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. How to keep decimal of a number when sending to a function
Forum Updated to NodeBB v4.3 + New Features

How to keep decimal of a number when sending to a function

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 364 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.
  • S Offline
    S Offline
    SeyMohsenFls
    wrote on last edited by
    #1

    Hi guys. I have a code that generates some numbers with high decimal. When I put these numbers in a vector and send it to a function, sometimes I feel that some decimals are lost and they are not transmitted exactly as they are. I send my numbers like this:

    long double aa=-k1*k2*k3*kw;
    long double bb=-(2*k1*k2*k3*Cs+k1*k2*kw+3*k1*k2*k3*Ca_new);
    long double cc= -(k1*k2*Cs+2*k1*k2*Ca_new+k1*kw-k1*k2*k3);
    long double dd=(k1*k2-k1*Ca_new-kw);
    long double ee=(k1+Cs);
    
    vector<long double>_coeff;
    _coeff.clear();
    _coeff.push_back(5);//degree
    _coeff.push_back((long double)aa);
    _coeff.push_back((long double)bb);
    _coeff.push_back((long double)cc);
    _coeff.push_back((long double)dd);
    _coeff.push_back((long double)ee);
    _coeff.push_back((long double)1);
    long double max=Roots(_coeff);
    

    I have also tried different modes like the following code:

    _coeff.push_back(-k1*k2*k3*kw);
    
    JonBJ 1 Reply Last reply
    0
    • S SeyMohsenFls

      Hi guys. I have a code that generates some numbers with high decimal. When I put these numbers in a vector and send it to a function, sometimes I feel that some decimals are lost and they are not transmitted exactly as they are. I send my numbers like this:

      long double aa=-k1*k2*k3*kw;
      long double bb=-(2*k1*k2*k3*Cs+k1*k2*kw+3*k1*k2*k3*Ca_new);
      long double cc= -(k1*k2*Cs+2*k1*k2*Ca_new+k1*kw-k1*k2*k3);
      long double dd=(k1*k2-k1*Ca_new-kw);
      long double ee=(k1+Cs);
      
      vector<long double>_coeff;
      _coeff.clear();
      _coeff.push_back(5);//degree
      _coeff.push_back((long double)aa);
      _coeff.push_back((long double)bb);
      _coeff.push_back((long double)cc);
      _coeff.push_back((long double)dd);
      _coeff.push_back((long double)ee);
      _coeff.push_back((long double)1);
      long double max=Roots(_coeff);
      

      I have also tried different modes like the following code:

      _coeff.push_back(-k1*k2*k3*kw);
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @SeyMohsenFls said in How to keep decimal of a number when sending to a function:

      When I put these numbers in a vector and send it to a function, sometimes I feel that some decimals are lost and they are not transmitted exactly as they are. I send my numbers like this:

      That does not happen. You have a vector<long double>, if you put long doubles into it that is what it will store.

      Your long double variables' assignments will only receive a long double if at least one of the variables on the right-hand side of the assignment is itself a long double. Is that always the case? Meanwhile, what eveidence do you have for "I feel" ?

      This question has nothing to do with Qt, just C++.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SeyMohsenFls
        wrote on last edited by
        #3

        I used "feel" because the value I get from the function is incorrect. While the function has no problems. So there may be a problem with sending numbers.

        JonBJ 1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #4

          Are you seeing a valid root but not the expected one?

          The answer(s) (there may be more than one for a fifth-order polynomial) are very much dependent on the characteristics of your root finding algorithm. Some numeric algorithms get stuck in local minima, oscillate around a root without converging quickly, stop after a certain number of iterations, stop when a certain error threshold is reached, control precision loss in different ways etc. Matching the algorithm to the typical inputs is important in some applications.

          Depending on the platform, a long double may be the same as a double in terms of decimal digit precision. To find out:

          #include <iostream>
          #include <limits>
          int main(){
              std::cout << std::numeric_limits<double>::digits10  << " " << std::numeric_limits<long double>::digits10 << std::endl;
          }
          

          Gives 15 and 18 on my x86_64 machine with GCC.

          The C-style static casts in the loading of the std::vector are unnecessary given all the input variables are declared long double.

          1 Reply Last reply
          4
          • S SeyMohsenFls

            I used "feel" because the value I get from the function is incorrect. While the function has no problems. So there may be a problem with sending numbers.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @SeyMohsenFls said in How to keep decimal of a number when sending to a function:

            because the value I get from the function

            What "function"? I don't particularly see any functions in your code above.

            is incorrect

            Incorrect in what specific way? How do you know it is "incorrect"?

            So there may be a problem with sending numbers.

            C++ does not work like that. It does not "mostly get things right, but has a problem/goes wrong when you 'send numbers'". Else programs would be pretty unreliable!

            Your code just shows a few calculations. No inputs specified, no outputs expected, no information about what you actually see instead or how you see it. Just "a feeling". Whether you want help here or (better) in a C++ forum, you need to produce a minimal example which actually runs and shows values which differ from you expect.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              Have a look at the numbers inside a debugger. I bet these are correct.

              You don't provide enough information how you check the numbers. My guess is that you somehow print the on the console. Per default C++ displays 6 digits (or so). This is what @ChrisW67 points to with getting the number of digits10. You need to output your numbers with at least these many digits (and more digits don't help) to know the exact number. Remember that computers use base 2 instead of base 10 and not every number can be perfectly represented in base 2 (e.g. try 0.2).

              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