Thread: exp: OVERFLOW error

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    16

    exp: OVERFLOW error

    Hi all,
    I am using Borland C++ 4.5 on 32-bit windows machine. I am getting "exp: OVERFLOW error" for my code. can anybody tell me how to deal with it.

    I am not posting my

    Code:
    struct MATRIX {
    	int rows;
    	int cols;
    	double **t;
    };
    
    ...
    ...
    
    double norm(struct MATRIX t, double s){...} 
    \\returns s-norm
    
    double scalar(struct MATRIX t){...} 
    \\returns the only element for 1X1 matrix
    
    struct MATRIX ind_response(struct MATRIX t, int index){...}
    \\returns a structure containing a column matrix having t.cols number of rows
    
    struct MATRIX multifun(struct MATRIX a, struct MATRIX b, char ch)
    \\return multiplied value of a.t and b.t matrices when ch == '*'
    
    struct MATRIX transpose(struct MATRIX t)
    \\returns transpose of matrix
    
    double fun(struct MATRIX x, struct MATRIX y, struct MATRIX z, int l){
    	double fval, sum = 0;
    	float bx, ybx;
    	int i;
    
    	for(i = 0; i < y.rows; i++){
    		bx = scalar(multifun(transpose(z), ind_response(x, i), '*'));
    		ybx = *(*(y.t+i)+0)* bx;
    		sum = sum - (ybx - log(1 + exp(bx)));
    		}
    	fval = sum + l* norm(z, 2.0);
    	return fval;
    	}
    ...
    
    main(){
    ...
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main returns int. Plus \\ is not a comment. // is. Although in C, it's /* and */.
    And you failed to mention where the problem is?
    But overflow means you need a bigger data type, that's all. That or you need to stop doing ridiculously high calculations so the number won't overflow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    Thanks for the suggestion.

    Sorry for my silly mistakes. I have given the comments for describing the functions for this thread.
    Basically I dont know where the problem is. Borland displays a pop-up error message. I am showing this part of the code because only here i use math functions.
    the data type i used is double. should i use long double?

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    If you code overflows, it made a number that was too big for it to store in whatever datatype you were trying to store the number in. It's possible that you're doing "ridiculously high calculations" (Obviously factorials would fall into this category), but since doubles numbers up to something ridiculously high like 10^300 or something, I think there could be other problems. Some things that come to mind are that you made a typo on one of the calculations and it's doing unexpected things. Also, if you don't initialize the number, it could start out really large. If you're sure that you aren't making any errors, you can use a bigger type. long double could be a solution but of course that's not guarenteed because it depends on the compiler and target machine. And you could always make your own way of handling floating point numbers.
    [edit]Post your entire code. If it's really big, attach it[/edit]
    Don't quote me on that... ...seriously

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the value of "bx" when it fails? That is presumably the cause of the problem. Using "long double" if the compiler supports that may solve the problem, but if you have a large enough value, any floating point type will overflow.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    I think this is due to exp() function which is unable to handle large float value. Consider the code below:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    #define z 8113.48
    
    main(){
    	long double x;
    	x = exp(z);
    	printf("%Lf", x);
    	return 0;
    	}
    this also displays same overflow error. Now please suggest me how to handle large float value in this case.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For the love of... I thought I taught you to use int main. Please use int main!
    Long double is the largest data type C can handle. x is initialized, as well.
    &#37;Lf is not used to print doubles either. %f is.
    And if you get overflow, it means you're doing too high calculations, simply put.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    Is it correct?
    Code:
    #include<stdio.h>
    #include<math.h>
    
    #define z 8113.48
    
    int main(){
    	long double x;
    	x = exp(z);
    	printf("%f", x);
    	return 0;
    	}
    But %f is not working !!!!!
    Also my problem is unsolved.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    &#37;f works. You're getting overflow, and there's not much to do about that unless you magically invent another type that holds larger numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    According to Windows Calculator 2.72 to the power of 8113 is 4.5e+3525, which is WAY outside of the range for double (by about ten orders of magnitude). If you can find a "exp()" function that takes a long double, you may be able to get it to work. According to "google man exp", the function you want could be called "expl()" - but that may depend on the compiler library, so don't hold me responsible if that doesn't work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Elysia: Note that the value is "long double", which does indeed need to be prefixed to %lf or some such. Which is correct depends on the compiler version (I think C99 standardizes it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    News to me, but anyway, as mentioned, the problem is overflow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    News to me, but anyway, as mentioned, the problem is overflow.
    Yes, having the wrong format for printf() will only change how printf behaves, not how the floating point unit and the exp() function behaves, that is correct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    expl() also not working. Suppose i want to create a new data type that can handle large floating values. Since i have no idea about this area...can anybody lighten me up?

    Another thing, what is the range of x in exp(x) for double and long double data types. I'm using Borland C++ 4.5.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can suggest you don't use Borland, if you can.
    See cpwiki.sf.net/Integrated_Development_Environment for a list of other IDEs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM