Is It Possible to Represent the Fractional Part of a Number as an Integer?

430 Views Asked by At

This question may not even have an answer in mathematics, so I apologize if it is too dumb.

I am writing an algorithm to order a series of numbered items. These items have a unique numbering style in which the decimal part of the number is treated as an integer and sorted accordingly.

For example, this is the correct order for the following items:

6, 6.1, 6.3, 6.10, 6.11, 6.30, 6.45, 7

In this example, the ".1" part is treated as the integer 1, which is less than the ".10" part, which translates to the integer 10.

I understand that this makes no sense from a standard mathematical point of view, since .10 is equivalent to .1, and 6.11 is normally less than 6.3.

Is there a mathematical notation that can treat the decimal part of a number as an integer? Or is there a way of representing the "numbers" 6.11 and 6.3 in some numeric way that follows the required sorting style? Sure, I can do something like set the maximum number of decimal places, and then translate ".1" to ".001", etc. I'm just wondering if there is a more direct approach.

The standard math coding tools don't work with this data, because the numbers I am using really are not numbers at all; they are something else. I can use string coding tools to sort these numbers correctly, but then I have to resort to special tricks to alphabetize the numbers correctly.

2

There are 2 best solutions below

1
On

You can split the string around the decimal point, then parse each part into an integer (there are library functions for this in most languages I've used) and sort by the first part and fall back to the second part to break ties.

0
On

What you need is fixed-point arithmetic. Floating point decimals are more common, but fixed-point representation is often found in applications that involve digital signaling processors (DSPs).

The idea is similar to what others proposed - split the number to integer and decimal part. Read https://en.wikipedia.org/wiki/Fixed-point_arithmetic for more information.