OJ says DBL_MAX is not declared even though I have included cfloat


  • 0
    V
    #include <iostream>
    #include <cerrno>
    #include <cfloat>
    #include <climits>
    #include <cmath>
    
    using namespace std;
    class Solution {
    public:
        double pow(double x, int n) {
            if(x == 0) {
    			if(n == 0) errno = EDOM;
    			if(n < 0) errno = ERANGE;
    			return 0;
    		}
    		if(n == 0) return 1;
    		if(n < 0) {
    			if(n == INT_MIN) {
    				double q = pow(x, INT_MAX);
    				return x/q;
    			}
    			else {
    				double q = pow(x, 0-n);
    				if(q == DBL_MAX || q == -DBL_MAX) return 0;
    				if(q == DBL_MIN || q == -DBL_MIN || q == 0) {
    					return (x>0?DBL_MAX:-DBL_MAX);
    				}
    				return 1/q;
    			}
    		}
    		if(x == 1)return 1;
    		if(x == -1) return (n%2 == 0?1:-1);
    		double p = x;
    		int m = 1;
    		int cnt = 0;
    		while(m <= n/2) {
    			m = 2*m;
    			cnt++;
    		}
    		int diff = n - m;
    
    		while(cnt != 0) {
    			if(fabs(p) < fabs(DBL_MAX/p)) p = p * p;
    			else {
    				if(x > 0 ) return DBL_MAX;
    				if(x < 0) {
    					if(n % 2 == 0) return DBL_MAX;
    					else return -DBL_MAX;
    				}
    			}
    			cnt--;
    		}
    
    		while(diff > 0) {
    			if(fabs(p) < fabs(DBL_MAX/x)) p = p * x;
    			else {
    				if(x > 0 ) return DBL_MAX;
    				if(x < 0) {
    					if(n % 2 == 0) return DBL_MAX;
    					else return -DBL_MAX;
    				}
    			}
    			diff--;
    		}
    		return p;
        }
    };

  • 0
    L

    God!!you can only write a function or a class.Can you pass the compiling?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.