CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    13

    Angry 0 isn't 0 in C++ math

    OK, i have a script, which calculate Y=1/(C-A). C is input, A is intetrval [Ap;Ag] with step Az;
    And i have one problem, when c=2, A[-1.5;2.5] with step 0.1, when i want to say that 0 isn't possible in 1/0, the program colculate me: Y = -2.1e+06 . Programm is not English language, sorry. Problem in answer line 36.

    Code:
    /*
     */
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    int main ()
    {
        float y, c, ap, az, ag;
    
    	cout << "Iveskite kintamojo C reiksme.\n"; 
    	cin >> c; //įvedame C reikšmę
    	do {
    		cout << "Iveskite intervalo pradzia ir gala.\nPradzia ir galas sutapti negali!\n";
    	    cin >> ap >> ag; //įvedame intervalo pradžią ir galą
    	} while (ap == ag); //išsireikalaujame, kad intervalo pradžia ir galas nesutaptų   
      	do {
    		cout << "Iveskite intervalo kitimo zingsni, kuris nelygus 0.\n";
    		cin >> az; //įvedame intervalo kitimo žingsnį
    	} while (az == 0.0); //išsireikalaujame, kad intervalo kitimo ˛ingsnis būtų nelygus 0
    	if (ag > ap && az > 0.0) { //jei intervalas didėjantis IR žingsnis DIDESNIS už 0, skaičiuojame
    		cout << setfill('-') << setw(58) << "-" << endl;
    		cout << setfill(' ') << setw(8)<<"Sprendinio nr. " <<"|  "<<"Atsakymas\n";
    		cout << setfill('-') << setw(58) << "-" << setfill(' ') <<endl;
    		for (float z = 1.0, a = ap; a <= ag; a += az) {
    			if (a == c) { //jei vardiklis lygus 0
    				cout << setfill(' ') << setw(14) << z << ".  ";
                    cout << "Kai A = " << a <<", o " << c << " - " << a <<" = 0, apskaiciuoti\nnegalima, nes dalyba is 0 negalima.\n";
    			}
    			else {
    				y = 1.0 / (c - a); //skaičiuojame
    				cout << setfill(' ') << setw(14) << z << ".  " << "Y = "  << setprecision(3) << y << endl;	
    			}
    			z++; //prisideame vienetuką prie sprendinio nr.
    		}	
    	
    	}
    	if (ag < ap && az < 0.0) { //jei intervalas mažėjantis IR žingsnis MAŽESNIS uz 0
    		cout << setfill('-') << setw(58) << "-" << endl;
    		cout << setfill(' ') << setw(8)<<"Sprendinio nr. " <<"|  "<<"Atsakymas\n";
    		cout << setfill('-') << setw(58) << "-" << setfill(' ') <<endl;
    		for (float z = 1.0, a = ap; a >= ag; a += az) {
    			if (a == c) { //jei vardiklis lygus 0
    				cout << setfill(' ') << setw(14) << z << ".  ";
                    cout << "Kai A = " << a <<", o " << c << " - " << a <<" = 0, apskaiciuoti\nnegalima, nes dalyba is 0 negalima.\n";
    			}
    			else {
    				y = 1.0 / (c - a); //skaičiuojame
    				cout << setfill(' ') << setw(14) << z << ".  " << "Y = "  << setprecision(3) << y << endl;	
    			}
    			z++; //prisideame vienetuką prie sprendinio nr.
    		}	
    		
    	}
    	if (ag > ap && az < 0.0) { //jei intervalas didėjantis IR žingsnis MAŽESNIS uz 0
    		cout << setfill('-') << setw(58) << "-" << endl;
    		cout << "Apskaiciuoti negalime,\nnes intervalas didejantis,\no zingsnis mazejantis.\n";
    	}			
    	if (ag < ap && az > 0.0) { //jei intervalas mažejantis IR žingsnis DIDESNIS už 0
    		cout << setfill('-') << setw(58) << "-" << endl;
    		cout << "Apskaiciuoti negalime,\nnes intervalas mazejantis,\no zingsnis didejantis.\n";
    	}			
    	cout << setfill('-') << setw(58) << "-" << setfill(' ') <<endl;
    	system ("pause");
    	return 0;
    }
    Code:
    Iveskite kintamojo C reiksme.
    2
    Iveskite intervalo pradzia ir gala.
    Pradzia ir galas sutapti negali!
    -1.5
    2.5
    Iveskite intervalo kitimo zingsni, kuris nelygus 0.
    0.1
    ----------------------------------------------------------
    Sprendinio nr. |  Atsakymas
    ----------------------------------------------------------
                 1.  Y = 0.286
                 2.  Y = 0.294
                 3.  Y = 0.303
                 4.  Y = 0.313
                 5.  Y = 0.323
                 6.  Y = 0.333
                 7.  Y = 0.345
                 8.  Y = 0.357
                 9.  Y = 0.37
                10.  Y = 0.385
                11.  Y = 0.4
                12.  Y = 0.417
                13.  Y = 0.435
                14.  Y = 0.455
                15.  Y = 0.476
                16.  Y = 0.5
                17.  Y = 0.526
                18.  Y = 0.556
                19.  Y = 0.588
                20.  Y = 0.625
                21.  Y = 0.667
                22.  Y = 0.714
                23.  Y = 0.769
                24.  Y = 0.833
                25.  Y = 0.909
                26.  Y = 1
                27.  Y = 1.11
                28.  Y = 1.25
                29.  Y = 1.43
                30.  Y = 1.67
                31.  Y = 2
                32.  Y = 2.5
                33.  Y = 3.33
                34.  Y = 5
                35.  Y = 10
                36.  Y = -2.1e+06
                37.  Y = -10
                38.  Y = -5
                39.  Y = -3.33
                40.  Y = -2.5
                41.  Y = -2
    ----------------------------------------------------------
    Last edited by justutiz; May 19th, 2013 at 01:05 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by justutiz View Post
    OK, i have a script, which calculate ...
    Scripts are not discussed in this Forum.
    There is a Visual C++ Programming forum, and Visual C++ is code, not script.

    Quote Originally Posted by justutiz View Post
    ... which calculate Y=1/(C-A). C is input, A is intetrval [Ap;Ag] with step Az;
    Did you, perhaps, mean "A is a value within intetrval [Ap;Ag]"?

    Anyway, have a look at:
    Floating point
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    and a lot of other articles/discussions here
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by VictorN View Post
    Scripts are not discussed in this Forum.
    There is a Visual C++ Programming forum, and Visual C++ is code, not script.

    Did you, perhaps, mean "A is a value within intetrval [Ap;Ag]"?

    Anyway, have a look at:
    Floating point
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    and a lot of other articles/discussions here
    Im reading, and Im very new in C++, so I dont find how I can do the folowing that a == c, then a and c is floats.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by justutiz View Post
    ... so I dont find how I can do the folowing that a == c, then a and c is floats.
    In general you cannot just compare floats as "a == c". You have to implement some relative small (with respect to the "a" and "c") value ("delta") and compare like:
    Code:
    if(abs(a - c) < delta)
    {
       ...
    }
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    but abs want me to use int, my a and c is floats

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: 0 isn't 0 in C++ math

    Well, I meant this abs, _abs64

    However, you can use fabs instead.
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: 0 isn't 0 in C++ math

    when i want to say that 0 isn't possible in 1/0, the program colculate me: Y = -2.1e+06
    Result of 1/0 is infinity, while programming languages operate with only finite figures. So you have the best result FPU was able to provide.
    Best regards,
    Igor

  8. #8
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by Igor Vartanov View Post
    Result of 1/0 is infinity, while programming languages operate with only finite figures. So you have the best result FPU was able to provide.
    Yes, but why in my code -0.1 + 0.1 is not 0?

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by justutiz View Post
    Yes, but why in my code -0.1 + 0.1 is not 0?
    I don't know what's wrong with your code, but mine works fine:
    Code:
    #include <stdio.h>
    
    int main()
    {
        float a = -0.1;
        printf("%f", a +0.1);
        return 0;
    }

    D:\Temp\8>8.exe

    -0.000000
    Best regards,
    Igor

  10. #10
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    Im still geting
    Code:
    1      -1      0
    1.11      -0.9      0
    1.25      -0.8      0
    1.43      -0.7      0
    1.67      -0.6      0
    2      -0.5      0
    2.5      -0.4      0
    3.33      -0.3      0
    5      -0.2      0
    10      -0.1      0
    -1.34e+07      7.45e-08      0
    -10      0.1      0
    -5      0.2      0
    -3.33      0.3      0
    -2.5      0.4      0
    -2      0.5      0
    -1.67      0.6      0
    -1.43      0.7      0
    -1.25      0.8      0
    -1.11      0.9      0
    trying everything, and still dont get how to fix it.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <math.h>
    
    using namespace std;
    int main ()
    {
        float y, c, ap, ag;
        c = 0;
        ap = -1;
        ag = 1;
        float az = 0.1;
        if (ag > ap && az > 0.0) { 		
    		for (float a = ap; a <= ag; a += az) {
    			if ( fabsf(c - a) < 0.0 && fabsf(c - a) > 0.0 ) {
                    cout << "1/0 not posible";
    			}
    			else {
    				y = 1.0 / (c - a);
                    cout  << setprecision(4) << y <<"      " << a << "      " << c << endl;	
    			}
    		}	
            
    	}
        else cout << "STOP";
    	system ("pause");
    	return 0;
    }

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: 0 isn't 0 in C++ math

    It's impossible to succeed with the condition fabsf(c - a) < 0.0. Victor meant something like fabsf(c - a) < 0.00001.
    Best regards,
    Igor

  12. #12
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by Igor Vartanov View Post
    It's impossible to succeed with the condition fabsf(c - a) < 0.0. Victor meant something like fabsf(c - a) < 0.00001.
    Yes, it works, but maybe it is shortes way to compare c-a is 0?

  13. #13
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    so
    Code:
    if ( fabsf(c - a) < 0.000001 )
    is the best and shortest way?

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by justutiz View Post
    so
    Code:
    if ( fabsf(c - a) < 0.000001 )
    is the best and shortest way?
    I'd say it is the only way. The question is only about this 'delta' value. In some case your choice with 0.000001 can work, in other - not. It all depends on the problem / values you are working on / with.
    Victor Nijegorodov

  15. #15
    Join Date
    Oct 2011
    Posts
    13

    Re: 0 isn't 0 in C++ math

    Quote Originally Posted by VictorN View Post
    I'd say it is the only way. The question is only about this 'delta' value. In some case your choice with 0.000001 can work, in other - not. It all depends on the problem / values you are working on / with.
    so it is posible to make working with all values, if az i take 0.000000001 then it is not working, so i need to add more 0.00000000000000001 in compare, it is other way?

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured