Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. avoiding or skipping Divide by zero showing junk values

avoiding or skipping Divide by zero showing junk values

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiocsharpc++
7 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    I have a console application which calculates using formulas based on the inputs it is receiving every cycle in a particular frequency and gives dynamic output values based on its input variables and constants and so on. In short its a real time application. This application was initially developed in Visual Studio 6.0 as c++ console application. Recently we converted it to Visual Studio 2008. It was working fine in VS 6.0. But after conversion, some of the values shows -1.#J which seems like infinite values. When I observed, it seems like certain calculations may result in divide by 0 or complex values like square root of a negative number and these values should show the value as -1.#J. But I wonder how there was no such values in VS6.0 whereas VS2008 shows these junk values. Please suggest me some ways of preventing it.

    H M L 4 Replies Last reply
    0
    • M manoharbalu

      I have a console application which calculates using formulas based on the inputs it is receiving every cycle in a particular frequency and gives dynamic output values based on its input variables and constants and so on. In short its a real time application. This application was initially developed in Visual Studio 6.0 as c++ console application. Recently we converted it to Visual Studio 2008. It was working fine in VS 6.0. But after conversion, some of the values shows -1.#J which seems like infinite values. When I observed, it seems like certain calculations may result in divide by 0 or complex values like square root of a negative number and these values should show the value as -1.#J. But I wonder how there was no such values in VS6.0 whereas VS2008 shows these junk values. Please suggest me some ways of preventing it.

      H Offline
      H Offline
      Heng Xiangzhong
      wrote on last edited by
      #2

      Please show your code. Everything is the code !

      1 Reply Last reply
      0
      • M manoharbalu

        I have a console application which calculates using formulas based on the inputs it is receiving every cycle in a particular frequency and gives dynamic output values based on its input variables and constants and so on. In short its a real time application. This application was initially developed in Visual Studio 6.0 as c++ console application. Recently we converted it to Visual Studio 2008. It was working fine in VS 6.0. But after conversion, some of the values shows -1.#J which seems like infinite values. When I observed, it seems like certain calculations may result in divide by 0 or complex values like square root of a negative number and these values should show the value as -1.#J. But I wonder how there was no such values in VS6.0 whereas VS2008 shows these junk values. Please suggest me some ways of preventing it.

        H Offline
        H Offline
        Heng Xiangzhong
        wrote on last edited by
        #3

        Please show your code. Everything is in the code !

        M 1 Reply Last reply
        0
        • H Heng Xiangzhong

          Please show your code. Everything is in the code !

          M Offline
          M Offline
          manoharbalu
          wrote on last edited by
          #4

          I am posting one of the functions used in the code. There are many functions similar to this in the entire application int CONDENSER(float Fl,float Fv,float Rc,float Gain,float DesFp,float Htcfl, float Htcmaxfl,float Tflsh,float Fc, float Cpcw, float Cppl, float Cppv, float Cpov,float Cpol, float DesFc, float Tcwi,float Tpfi,float Tc,float Lhpv,float Lhv, float Condeff,float CondHtarea,float Ambhc, float Ambhf, float Ambt,float Mhc, float Mhf, float Tfmax, float Tcmax,float *Liquid, float *Vapor,int IL, int IV,int OV, int OL,int FN,int CN) { int J,N,K,TYPE; float Fp,SYCOMP,SXCOMP,Heatin,Heatvap,Heatliq; float Heatloss,Hg,Hc,DELT1,DELT2,A,B,DENOM,DELT,Heatout; float DER,MAX,MIN,FLAG,P,I,PI; // Amount of Vapor condensed [ L = ( 1 - V/F) * F ] Fp = Fl + Fv; *Liquid = Rc * Fp; // By material balance *Vapor = Fp - (*Liquid); // Composition of Vapor in equilibrium with Liquid Y = K * X SYCOMP = 1.0E-10; for(J=Esim->COMPBEG;J<=Esim->COMPEND;J++) { Esim->SCOMP[J][OV] = Esim->NKVAL[J][FN]*Esim->NXS[J][FN]; SYCOMP = SYCOMP + Esim->SCOMP[J][OV]; } // Condensation rate (PI Logic) Esim->NS1DER[FN]= 1 - (SYCOMP); P = Gain*Esim->NS1DER[FN]; I = Esim->NS1S[FN]; I = LIMIT01(I+Esim->NODEK7[FN]*Esim->NS1DER[FN]); PI = LIMIT01(P+I); Esim->NS1S[FN] = LAG(PI,Esim->NS1S[FN],0.05); Esim->NS1S[FN] = LIMIT01(Esim->NS1S[FN]); // Condensed Liquid Composition // FEED * Yi - VAP * Yi - LIQ * Xi = ACC * (d(Xi)/dt)*dt // d(Xi) = (FEED * Yi - VAP * Yi - LIQ * Xi)/(ACC * dt) SXCOMP = 1.0E-10; for(J=Esim->COMPBEG;J<=Esim->COMPEND;J++) { Esim->SCOMP[J][OV] = Esim->SCOMP[J][OV]/SYCOMP; Esim->NXDER[J][FN] = (Fl*Esim->SCOMP[J][IL]+Fv*Esim->SCOMP[J][IV] -(Fv)*Esim->NXS[J][FN] -(Fl)*Esim->NXS[J][FN])/(10 + MAXI(Fp-(*Vapor),100.0) *Esim->STPSIZEH/10000); Esim->NXS[J][FN] = EULER( N, K, TYPE, DER, MAX, MIN, FLAG); SXCOMP = SXCOMP + Esim->NXS[J][FN]; } for(J=Esim->COMPBEG; J<=Esim->COMPEND; J++) { Esim->NXS[J][FN] = Esim->NXS[J][FN]/SXCOMP; Esim->SCOMP[J][OL] = Esim->NXS[J][FN]; } Hg = power((Fp/DesFp),0.5); // Efficiency of the condenser Esim->NS2[FN] = Condeff*MAXI(Htcfl*Hg,Htcmaxfl); Esim->NHTR[FN] = Esim->NS2[FN]*(Tflsh - Tc); if(Fc>0) // Cooling water side calculations { // From McCabe & Smith h=K*(Fc)**(-1/3) Hc = power((Fc/DesFc),-0.33); // Accounting for the overall Heat transfer coefficient // 1/U = 1/Hg + 1/Hc Esim->NS2[FN] = Co

          L 1 Reply Last reply
          0
          • M manoharbalu

            I am posting one of the functions used in the code. There are many functions similar to this in the entire application int CONDENSER(float Fl,float Fv,float Rc,float Gain,float DesFp,float Htcfl, float Htcmaxfl,float Tflsh,float Fc, float Cpcw, float Cppl, float Cppv, float Cpov,float Cpol, float DesFc, float Tcwi,float Tpfi,float Tc,float Lhpv,float Lhv, float Condeff,float CondHtarea,float Ambhc, float Ambhf, float Ambt,float Mhc, float Mhf, float Tfmax, float Tcmax,float *Liquid, float *Vapor,int IL, int IV,int OV, int OL,int FN,int CN) { int J,N,K,TYPE; float Fp,SYCOMP,SXCOMP,Heatin,Heatvap,Heatliq; float Heatloss,Hg,Hc,DELT1,DELT2,A,B,DENOM,DELT,Heatout; float DER,MAX,MIN,FLAG,P,I,PI; // Amount of Vapor condensed [ L = ( 1 - V/F) * F ] Fp = Fl + Fv; *Liquid = Rc * Fp; // By material balance *Vapor = Fp - (*Liquid); // Composition of Vapor in equilibrium with Liquid Y = K * X SYCOMP = 1.0E-10; for(J=Esim->COMPBEG;J<=Esim->COMPEND;J++) { Esim->SCOMP[J][OV] = Esim->NKVAL[J][FN]*Esim->NXS[J][FN]; SYCOMP = SYCOMP + Esim->SCOMP[J][OV]; } // Condensation rate (PI Logic) Esim->NS1DER[FN]= 1 - (SYCOMP); P = Gain*Esim->NS1DER[FN]; I = Esim->NS1S[FN]; I = LIMIT01(I+Esim->NODEK7[FN]*Esim->NS1DER[FN]); PI = LIMIT01(P+I); Esim->NS1S[FN] = LAG(PI,Esim->NS1S[FN],0.05); Esim->NS1S[FN] = LIMIT01(Esim->NS1S[FN]); // Condensed Liquid Composition // FEED * Yi - VAP * Yi - LIQ * Xi = ACC * (d(Xi)/dt)*dt // d(Xi) = (FEED * Yi - VAP * Yi - LIQ * Xi)/(ACC * dt) SXCOMP = 1.0E-10; for(J=Esim->COMPBEG;J<=Esim->COMPEND;J++) { Esim->SCOMP[J][OV] = Esim->SCOMP[J][OV]/SYCOMP; Esim->NXDER[J][FN] = (Fl*Esim->SCOMP[J][IL]+Fv*Esim->SCOMP[J][IV] -(Fv)*Esim->NXS[J][FN] -(Fl)*Esim->NXS[J][FN])/(10 + MAXI(Fp-(*Vapor),100.0) *Esim->STPSIZEH/10000); Esim->NXS[J][FN] = EULER( N, K, TYPE, DER, MAX, MIN, FLAG); SXCOMP = SXCOMP + Esim->NXS[J][FN]; } for(J=Esim->COMPBEG; J<=Esim->COMPEND; J++) { Esim->NXS[J][FN] = Esim->NXS[J][FN]/SXCOMP; Esim->SCOMP[J][OL] = Esim->NXS[J][FN]; } Hg = power((Fp/DesFp),0.5); // Efficiency of the condenser Esim->NS2[FN] = Condeff*MAXI(Htcfl*Hg,Htcmaxfl); Esim->NHTR[FN] = Esim->NS2[FN]*(Tflsh - Tc); if(Fc>0) // Cooling water side calculations { // From McCabe & Smith h=K*(Fc)**(-1/3) Hc = power((Fc/DesFc),-0.33); // Accounting for the overall Heat transfer coefficient // 1/U = 1/Hg + 1/Hc Esim->NS2[FN] = Co

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Firstly, please indent your code properly and use <pre> tags around it so that it is readable, like:

            for(J = Esim->COMPBEG; J <= Esim->COMPEND; J++)
            {
            Esim->SCOMP[J][OV] = Esim->SCOMP[J][OV]/SYCOMP;
            Esim->NXDER[J][FN] = (Fl*Esim->SCOMP[J][IL]+Fv*Esim->SCOMP[J][IV] -(Fv)*Esim->NXS[J][FN]
            -(Fl)*Esim->NXS[J][FN])/(10 + MAXI(Fp-(*Vapor),100.0)
            *Esim->STPSIZEH/10000);

            Esim->NXS\[J\]\[FN\] = EULER( N, K, TYPE, DER, MAX, MIN, FLAG);
            
            SXCOMP = SXCOMP + Esim->NXS\[J\]\[FN\];
            

            }

            Secondly, please indicate exactly where the error occurs, and show the values of the inputs at the time.

            1 Reply Last reply
            0
            • M manoharbalu

              I have a console application which calculates using formulas based on the inputs it is receiving every cycle in a particular frequency and gives dynamic output values based on its input variables and constants and so on. In short its a real time application. This application was initially developed in Visual Studio 6.0 as c++ console application. Recently we converted it to Visual Studio 2008. It was working fine in VS 6.0. But after conversion, some of the values shows -1.#J which seems like infinite values. When I observed, it seems like certain calculations may result in divide by 0 or complex values like square root of a negative number and these values should show the value as -1.#J. But I wonder how there was no such values in VS6.0 whereas VS2008 shows these junk values. Please suggest me some ways of preventing it.

              M Offline
              M Offline
              Munchies_Matt
              wrote on last edited by
              #6

              You don't want to do that, you want to avoid device by zero period. If you don't know why, go back to school.

              "The whole idea that carbon dioxide is the main cause of the recent global warming is based on a guess that was proved false by empirical evidence during the 1990s." climate-models-go-cold

              1 Reply Last reply
              0
              • M manoharbalu

                I have a console application which calculates using formulas based on the inputs it is receiving every cycle in a particular frequency and gives dynamic output values based on its input variables and constants and so on. In short its a real time application. This application was initially developed in Visual Studio 6.0 as c++ console application. Recently we converted it to Visual Studio 2008. It was working fine in VS 6.0. But after conversion, some of the values shows -1.#J which seems like infinite values. When I observed, it seems like certain calculations may result in divide by 0 or complex values like square root of a negative number and these values should show the value as -1.#J. But I wonder how there was no such values in VS6.0 whereas VS2008 shows these junk values. Please suggest me some ways of preventing it.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                One possible reason of many is that in the original (vc++ 6) structured exception handling was enabled and now it isn't. This would mean that divide by 0 exceptions were being thrown but not being caught (possibly). Now they are just crashing the program. Unhandled divide by zero is bad so don't worry about why it did/did not. Just fix the problem now. Structured exception handling is a very good way of doing that.

                Peter Wasser "The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups