• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Float vs double

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 26751
190
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 26751
190
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
eat bricks! HA! And here's another one! And a tiny ad!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic