Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float vs double

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing some mathematical calculations. My calculations will definitely involve decimals. I'll be using java.lang.Math class also. For precision I want to use double but I am afraid of performance. Is there an alternative or a best practice that I should take care of?

Thanks
Imad
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there's the old "Don't optimize prematurely" rule. First get the code correct and get it clean and simple. That would make it easier to optimize IFF you need to,

If you need double precision, you need double precision. Losing data just to gain speed is false economy.

There are some basics, of course, and they apply to any computing, not just mobile. Stuff like moving invariant calculations out of loops so that you don't waste time re-calculating the exact same numbers over and over again.

On the other hand, if you have some serious number crunching to do, like, say simulating the weather for the next 90 days, pocket devices aren't intended to handle that much load. For that, you would be better off doing the number crunching on a heavy-duty backend server machine and feeding the data from the mobile device.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read recently (PragProg Hello, Android book) that the KVM doesn't actually use hardware to do quick primitive calculations but instead does the calculations at the software level, which makes it take a little longer than normal Java.

I'm not sure many apps out there would be doing enough math to notice a performance hit, but I still think it's interesting.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Tim. You are right. I should first get my code working and then think about optimizing it.
Marc, that's exactly what I read and that's why I was concerned. I'll definitely post when I am done with my problem on how bad/good using double could be.

Thanks
Imad
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of Java's Write-Once/Run-Anywhere architecture, more often than not JVMs will do floating-point in software and ignore any built-in floating-point hardware, because relatively few systems actually have IEEE-compliant processors. IBM's mainframe line, in fact has a quirky FP architecture all its own, although since IBM loves Java so much, they added a second FP processor just for the sake of having native Java FP.

Portable devices might not even have a hardware FP processor - the extra circuitry required means using up chip real estate and power, and both are at a premium for small devices. Especially power!

But if you need FP, you need FP, and if you need precision, use "double".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic