0

I'm using a for-loop to determine whether the long double is an int. I have it set up that the for loop loops another long double that is between 2 and final^1/2. Final is a loop I have set up that is basically 2 to the power of 2-10 minus 1. I am then checking if final is an integer. My question is how can I get only the final values that are integers? My explanation may have been a bit confusing so here is my entire loop code. BTW I am using long doubles because I plan on increasing these numbers very largely.

for (long double ld = 1; ld<10; ld++) {
    long double final = powl(2, ld) - 1;
    //Would return e.g. 1, 3, 7, 15, 31, 63...etc.

    for (long double pD = 2; pD <= powl(final, 0.5); pD++) {
    //Create new long double
    long double newFinal = final / pD;
    //Check if new long double is int
    long int intPart = (long int)newFinal;
    long double newLong = newFinal - intPart;

    if (newLong == 0) {
        NSLog(@"Integer");
        //Return only the final ints?
    }
}
}
Milo
  • 5,041
  • 7
  • 33
  • 59
  • @nhgrif sorta has something. You should be able to take the number, cast to long, cast back to double, and then compare the two bit-for-bit. The only other thing you'd need to check is that the number of bits "used" in the long is fewer than the size of double's mantissa. – Hot Licks Dec 12 '13 at 02:18
  • Oops -- I see you're using a `long double`, not casting between `long` and `double`. Perhaps you could cast to a `long long`? – Hot Licks Dec 12 '13 at 02:20

1 Answers1

1

Just cast it to an int and subtract it from itself?

long double d;
//assign a value to d

int i = (int)d;
if((double)(d - i) == 0) {
    //d has no fractional part
}

As a note... because of the way floating point math works in programming, this == check isn't necessarily the best thing to do. Better would be to decide on a certain level of tolerance, and check whether d was within that tolerance.

For example:

if(fabs((double)(d - i)) < 0.000001) {
    //d's fractional part is close enough to 0 for your purposes
}

You can also use long long int and long double to accomplish the same thing. Just be sure you're using the right absolute value function for whatever type you're using:

  • fabsf(float)
  • fabs(double)
  • fabsl(long double)

EDIT... Based on clarification of the actual problem... it seems you're just trying to figure out how to return a collection from a method.

-(NSMutableArray*)yourMethodName {
    NSMutableArray *retnArr = [NSMutableArray array];

    for(/*some loop logic*/) {
        // logic to determine if the number is an int

        if(/*number is an int*/) {
            [retnArr addObject:[NSNumber numberWithInt:/*current number*/]];
        }
    }

    return retnArr;
}

Stick your logic into this method. Once you've found a number you want to return, stick it into the array using the [retnArr addObject:[NSNumber numberWithInt:]]; method I put up there.

Once you've returned the array, access the numbers like this:

[[arrReturnedFromMethod objectAtIndex:someIndex] intValue];

Optionally, you might want to throw them into the NSNumber object as different types.

You can also use:

  • [NSNumber numberWithDouble:]
  • [NSNumber numberWithLongLong:]

And there are matching getters (doubleValue,longLongValue) to extract the number. There are lots of other methods for NSNumber, but these seem the most likely you'd want to be using.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Sorry but I already have this. I may have been unclear in my explanation. I know how to figure if its an int or not. Because d changes, I want to return only the d's that are ints. Thank you for the advice on checking if its 0. – Milo Dec 12 '13 at 02:14
  • You're going to have to massively clarify where you're stuck. The `if` will return true for any double that doesn't have a fractional part and `false` for a double that does. Use that logic to do whatever you want now that it's sorting numbers out. – nhgrif Dec 12 '13 at 02:19
  • Ok I have A. A is a double that has been ran through a loop and is now the numbers 1, 3, 7, 15, 31...etc. I now want to take A within its loop and divide it by another double, B, that has been ran through a loop. That double is 2 through A to the power of 1/2. We now have A/B. I have set A/B to the double C. Because all these values are changing I just want to NSLOG C when C is an int. Clear? – Milo Dec 12 '13 at 02:21
  • Is this in a method? You need to return a collection of numbers back to the caller? – nhgrif Dec 12 '13 at 02:22