示例#1
0
int newDiv(int a, int b)
{
	int isPos;
	int digits;
	long long x,y,tmp,p;
	isPos = 0;			//其值为1表示为正,0为负
	int res;
	
	x = (long long)a;
	y = (long long)b;
	
	if((x<0 && y<0) || (x>0 && y>0))
	{
		isPos = 1;
	}
	
	x = longAbs(x);
	y = longAbs(y);

	res = 0;	
	while(x>=y)
	{
		tmp = y;
		p = 1;
		while(x>(tmp<<1))
		{
			tmp <<= 1;
			p <<= 1;	
		}
		res += p;
		x -= tmp;
	}
	
	if(isPos==0)
	{
		res = 0 - res;
	}	
	return res;
}
示例#2
0
extern "C" Box* abs_(Box* x) {
    if (isSubclass(x->cls, int_cls)) {
        i64 n = static_cast<BoxedInt*>(x)->n;
        return boxInt(n >= 0 ? n : -n);
    } else if (x->cls == float_cls) {
        double d = static_cast<BoxedFloat*>(x)->d;
        return boxFloat(d >= 0 ? d : -d);
    } else if (x->cls == long_cls) {
        return longAbs(static_cast<BoxedLong*>(x));
    } else {
        static const std::string abs_str("__abs__");
        return callattr(x, &abs_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = false }), ArgPassSpec(0),
                        NULL, NULL, NULL, NULL, NULL);
    }