Question

I'm trying to reverse a double value like this:

Input: 1020304050...... Output: 5040302010 the group of 2 digits remain in the same order. So 10 doesn't become 01. or 53 doesn't become 35.

The input will always have even number of digits, so pairing isn't an issue.

The 2 adjacent digits are actually a code number for a function. And I want to maintain that so I can apply that function again.

double temp = 0.0;
    double Singlefilter=1;
    double reverseString=0;
    temp=filtersequence;

    while (temp>0)
    {


        Singlefilter=fmod(temp, 100.00);
        temp=temp/100;

        reverseString=(reverseString+Singlefilter)*100;
        NSLog(@"reversed string of filter %f",reverseString);

    }

But I have no idea why this isn't working. This is generating randomly very very big values.


[This question has been replaced by Reverse a double value while maintaining 2 adjacent digits in same format]

Was it helpful?

Solution 2

You're better off just converting it to a string and reversing it.

NSString *inputString = [@(input) stringValue]; // Eg 1230
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [inputString length];
while (charIndex > 0) {
    charIndex--;
    NSRange subStrRange = NSMakeRange(charIndex, 1);
    [reversedString appendString:[inputString substringWithRange:subStrRange]];
}
double result = [reversedString doubleValue]; // Eg 0321 -> 321
// Go back to NSString to get the missing length
NSString *resultString = [@(result) stringValue]; // Eg. 321

// Multiple by factors of 10 to add zeros
result *= exp(10, [inputString length] - [resultString length]); // 3210

NSLog(@"reversed value %f", result);

Reverse string method from this answer.

OTHER TIPS

You can do it like this:

#import <Foundation/Foundation.h>
#include "math.h"

void outputGroupReversed(double filtersequence)
{
    double reverseString = 0.0;
    double temp = filtersequence;

    while (temp > 0.0)
    {
        double groupMultiplier = 100.0;
        double singleFilter = fmod(temp, groupMultiplier);
        temp = floor(temp / groupMultiplier);

        reverseString = reverseString * groupMultiplier + singleFilter;
    }

    NSLog(@"reversed string of filter %f", reverseString);
}


int main(int argc, const char * argv[])
{
    @autoreleasepool {

        outputGroupReversed(1020304050.0);

    }

    return 0;
}

This code does not handle input with a fractional part correctly, though.

If you wish to store 2-digit decimal integers packed together as a single numeric value you would be better of using uint64_t - unsigned long 64 bit integers rather than double. That will store 9 pairs of two decimal digits precisely which appears to be one more than you need (you mention 16 digits in a comment).

As long as the numeric value of the packed pairs is not important, just that you can pack 8 2-digit decimal numbers (i.e. 0 -> 99) into a single numeric value, then you can do better. A 64-bit integer is 8 pairs of 2-hexadecimal digit numbers, or 8 8-bit bytes, so you can store 8 values 0 -> 99 one per byte. Now adding and extracting values becomes bit-shifts (>> & << operators) by 8 and bitwise-or (|) and bitwise-and (&). This at least makes it clear you are packing values in, which divide & remainder do not.

But there is another payoff, your "reverse" operation now becomes a single call to CFSwapInt64() which reverses the order of the bytes.

However having said the above, you really should look at your model and consider another data type for what you are doing - long gone are the days when programs had to pack multiple values into words to save space.

For example, why not just use a (C) array of 8-bit (uint8_t values[8]) integers? If you require you can place that in a struct and pass it around as a single value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top