Infinite decimal places

Question:

How can I manipulate numbers with infinite decimal places?

Life Story:

I thought I saw something about this somewhere but I’ve forgotten where and I can’t seem to find it again. I’m referring to numbers like 0.3333… (1/3). I don’t actually need to store the number, I just need to modify it or avoid it all together. Even then, it’s not so much need as it’s being anal about something that is actually unimportant to my game and getting hung up over minor details. I know it has something to do about floats and doubles and stuff but this far exceeds my limited knowledge. Even still, I really dislike the words “It can’t be done.” Everything can be done, I just don’t know how yet. All science was magic at one time and all magic will be science eventually. And so on and so forth about irrelevant stuff… endRamble();

Code

10:30AM + 5 seconds in

[java]

System.out.println(“Time in: " + convertToSeconds(“10:30:05”));

System.out.println(“Time out: " + convertFromSeconds(convertToSeconds(“10:30:05”)));

[/java]

10:30AM + 4.9 seconds out

[java]

public String convertFromSeconds(float seconds) {

String time;

float conversion;

// Hours

conversion = seconds / 3600;

time = (int) Math.floor(conversion) + “:”;

conversion -= Math.floor(conversion);

// Minutes

conversion *= 60;

if (conversion < 10) {

time += “0”;

}

time += (int) Math.floor(conversion) + “:”;

conversion -= Math.floor(conversion);

// Seconds

conversion *= 600;

conversion = FastMath.floor(conversion) / 10;

time += conversion;

return time;

}

public float convertToSeconds(String time) {

int colPos = time.indexOf(”:”);

if (colPos <= 0) {

logger.log(Level.WARNING, “Invalid time format. Must be minimum H:M(1:0) up to HH:MM:SS.S(15:42:30.5). Returning 0.”);

return 0;

}

int hour = Integer.valueOf(time.substring(0, colPos));

time = time.substring(colPos + 1);

colPos = time.indexOf(":");

int minute;

float seconds;

if (colPos <= 0) {

minute = Integer.valueOf(time);

seconds = 0;

} else {

minute = Integer.valueOf(time.substring(0, colPos));

seconds = Float.valueOf(time.substring(colPos + 1));

}

return this.convertToSeconds(hour, minute, seconds);

}

public float convertToSeconds(int hour, int minute, float seconds) {

seconds += minute * 60;

seconds += hour * 3600;

return seconds;

}

[/java]

Some trivia (not much todo with your problem):

Some numbers can’t be expressed as fractions, they are called irrational numbers. PI is an example of an irrational number and can’t be manipulated with infinite decimals…

Rational numbers like 1/3 can be saved and manipulated in this fractional representation => “infinite decimals”.

Pi does however have a standard approximation, which is 22/7… This is only an approximation though.

As @kwando says some numbers just aren’t rational and there is no way to express them in finite ways.

That’s not the problem you are having hear though (or at least based on a quick glance at the code I don’t think so). It sounds like the problem you are seeing is due to float/double issues though - you really need to read up and understand how they work (for example why they should never ever be used for financial stuff).

Or just use BigDecimal…