Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Until Sun adds log-base-10, what do you do?  (Read 1824 times)
0 Members and 1 Guest are viewing this topic.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Posted 2005-12-27 15:36:11 »

 Sadly, in java, due to inaccuracies of double divisions, log10(1000) == 2. This is a bit of a bummer - basic algorithms completely fail when you can't even get log( 1000 ) to equal 3 Sad.

(NB: it actually comes out as 2.999999999999999996 - which, obviously, is wrong, however you look at it)

In the past, I've written entire *classes* to get around Java's crapness in this regards (yes, I know they have finally added log10 to java 5, thank god, but I still use 1.4 for everything), and it looks like I'm about to have to do so again, using hand-tweakable epsilons to enforce things like 2.999... == 3.

But, surely there's a better way? What do you use?

malloc will be first against the wall when the revolution comes...
Offline hvor2

Junior Duke




Beyond mind, there is an awareness...


« Reply #1 - Posted 2005-12-27 16:06:16 »

As a schooled 'FORTRANER'  (Faculty of Science), I will use hand-tweakable epsilons too. Maybe there is not more elegant method.
And I must say that I am shocked to hear that log10(1000) == 2 in java 1.4. I wasn't aware of that!

Offline nonnus29

Senior Duke




Giving Java a second chance after ludumdare fiasco


« Reply #2 - Posted 2005-12-27 17:26:23 »

Sadly, in java, due to inaccuracies of double divisions, log10(1000) == 2. This is a bit of a bummer - basic algorithms completely fail when you can't even get log( 1000 ) to equal 3 Sad.

I'm just curious, what are you coding such that you need to be using logarithms in the first place?  And why is 2.9999... not good enough?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2005-12-27 18:56:16 »

Sadly, in java, due to inaccuracies of double divisions, log10(1000) == 2. This is a bit of a bummer - basic algorithms completely fail when you can't even get log( 1000 ) to equal 3 Sad.

(NB: it actually comes out as 2.999999999999999996 - which, obviously, is wrong, however you look at it)

Its right to about 19 places which isnt bad.

And I fai lto see how that is 2 for anyting but very stupid truncation.  Mayvbe Im missing something but if your trucnating that to 2 rather then rounding to 3 I dont think you can blame Java....

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #4 - Posted 2005-12-27 19:10:37 »

I don't think it's that bad, just round it. Blame "Floating Point" math if you like. I am pretty sure you will get the same result in any C/C++ libraries too.

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #5 - Posted 2005-12-27 19:25:32 »

Its right to about 19 places which isnt bad.

The integer result of a log is - in every place I have ever seen it used in my life - the largest integer less than or equal to the actual non-integer result. i.e. always-round-down. i.e. java's own built-in int-conversion.

So, no - incorrect at 19 dec plac isn't "bad" - it's just completely wrong.

The rest of your post seems to be missing the point. Especially considering what you call "very stupid" is in fact java's very handy built-in behaviour. The *problem* with java is that log(A)/log(B) is extremely inaccurate for common real-life values of A and B, whereas a standard-library method of logB(A) for the world's most common values of B (2 and 10) would largely avoid these inaccuracies, and would more often get a correct answer, I believe.

malloc will be first against the wall when the revolution comes...
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #6 - Posted 2005-12-27 19:30:02 »

I don't think it's that bad, just round it. Blame "Floating Point" math if you like. I am pretty sure you will get the same result in any C/C++ libraries too.

Hmm. I thought people would understand what I meant by integer logs. Sorry, guys.

Here you go (hopefully this is self-evident why the above suggestion won't work without epsilons):

floating point: log-10( 999 ) == 2.999...
floating point: log-10( 1000 ) == 3

But, in java, they both equal 2.999(something)

int: log-10( 999 ) == 2
int: log-10( 1000 ) == 3

See?

malloc will be first against the wall when the revolution comes...
Offline Vorax

Senior Duke


Projects: 1


System shutting down in 5..4..3...


« Reply #7 - Posted 2005-12-27 19:46:43 »

I think what you need is the BigDecimal class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
    BigDecimal bd  = BigDecimal.valueOf( Math.log10(999d) );
    int intlog = bd.setScale(0, BigDecimal.ROUND_UP).intValue();

    System.out.println( intlog );

    bd  = BigDecimal.valueOf( Math.log10(1000d) );
    intlog = bd.setScale(0, BigDecimal.ROUND_UP).intValue();

    System.out.println( intlog );


Output is:
3
3

[edit] Use BigDecimal.ROUND_CEILING if you are rounding signed values towards positive infinity - will also work here in place of ROUND_UP [/edit]

[EDIT-EDIT] Just noticed you don't want to use JRE 1.5 for the Math.log10 call, that's ok though the BigDecimal round for 1.4 should still be better then an epsilon [/EDIT-EDIT]

Offline elias

Senior Duke





« Reply #8 - Posted 2005-12-27 22:09:45 »

I don't think it's that bad, just round it. Blame "Floating Point" math if you like. I am pretty sure you will get the same result in any C/C++ libraries too.

Hmm. I thought people would understand what I meant by integer logs. Sorry, guys.

Here you go (hopefully this is self-evident why the above suggestion won't work without epsilons):

floating point: log-10( 999 ) == 2.999...
floating point: log-10( 1000 ) == 3

But, in java, they both equal 2.999(something)

int: log-10( 999 ) == 2
int: log-10( 1000 ) == 3

See?

I don't get it. Floating point operations are only as accurate as the size of the floating point type and the computation in question, so 2.99999... is as good as it gets. Requiring the truncated (cast to int) value of a floating point value to behave like you describe would ruin the precision of any other (normal) floating point computation. What exactly is wrong with Math.round()? Sure a "direct" log10(x) for x = 1000 gives you a truncated 3 for this case, but you can't count on that for all values of x (esp. large ones). If you need the integer value, rounding is just as good.

 - elias

Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2005-12-28 06:03:05 »

Ditto on I don't get it.

It sounds to me like you are trying to get integer results out of a float operation.

So what youa re **really** complainign abotu is that there is no integer log function.

For the record, I don't remember other log functions being integer, in fact I seem to remember my log tablkes being full of decimal places.

Maybe I still don't understand you.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #10 - Posted 2005-12-28 14:09:54 »

depence on what you do with the rest of the equations

somethimes Approximations don't cut it. With the created errors can have annoying result. also mind that significance decreases fast wen doing multiplications.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline asc

Senior Newbie





« Reply #11 - Posted 2005-12-28 19:57:55 »

I assume you mean that you are actually doing Log(1000) / Log(10) which isn't exactly 3.

Instead of epsilons, how about writing your own log10 function that checks for input values of 10, 100, 1000... and returns integers, but otherwise returns Log(A) / Log(10).

EDIT:
The only reason I can think of for needing the integer part of a logarithm is to calculate the number of digits in a decimal representation. log10(i) is String.valueOf(i).length() - 1. So, what do you need it for?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (33 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (74 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!