14 Replies - 1516 Views - Last Post: 17 June 2011 - 06:17 PM Rate Topic: **--- 3 Votes

#1 R41D3NN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 14-June 11

What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 03:42 PM

So, I am working on a program that accurately describes a system of moving bodies and thus need to have some sort of data type that will give me the best results in terms of a body's position. So, a body has a center point p, where Point is a class that holds a value on each of the three coordinate axes, what should I use?
Also, I will be creating a ForceVector class that describes the movement of the bodies and contains operations for such mathematical computation. Is this appropriate for what I am doing and wanting to achieve?

Is This A Good Question/Topic? 0
  • +

Replies To: What is the Best Data Type For Accuracy?

#2 foofo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 19
  • Joined: 06-May 11

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 03:50 PM

Have a look at Java's primitive data types, and consider their advantages and disadvantages i.e. memory usage, maximum and minimum values etc.

Precision is the main difference.

[b][b][b]Float - 7 digits (32 bit)

Double -15-16 digits (64 bit)

Depends on your application really...

This post has been edited by foofo: 14 June 2011 - 04:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 03:51 PM

well usually for computers integer is pleanty fine for being accurate. It depends though. Are you going to be evaluating the points? or are you going to be displaying them on the screen?
Was This Post Helpful? 0
  • +
  • -

#4 R41D3NN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 14-June 11

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 03:53 PM

View Postimmeraufdemhund, on 14 June 2011 - 03:51 PM, said:

well usually for computers integer is pleanty fine for being accurate. It depends though. Are you going to be evaluating the points? or are you going to be displaying them on the screen?


Well, I am going to be evaluating these points. I have yet to get to the visual graphics part of coding.
Was This Post Helpful? 0
  • +
  • -

#5 R41D3NN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 14-June 11

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 03:58 PM

View Postfoofo, on 14 June 2011 - 03:50 PM, said:

Decimal -28-29 significant digits (128 bit)[/b][/b][/b]


So, I have not heard of the Decimal data type before, where can it be found? Is it a primitive in Java or is it in some package?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 04:29 PM

View PostR41D3NN, on 14 June 2011 - 06:58 PM, said:

View Postfoofo, on 14 June 2011 - 03:50 PM, said:

Decimal -28-29 significant digits (128 bit)[/b][/b][/b]


So, I have not heard of the Decimal data type before, where can it be found? Is it a primitive in Java or is it in some package?

Me neither, probably a typo in foofo replied. He actually edited his reply
Was This Post Helpful? 1
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 04:37 PM

The most precise has to be BigDecimal, but it will also be the slowest option and the most awkward to code with. double is pretty precise and is very good for scientific and engineering calculations, but it suffers from rounding errors when converting it to decimal for display.
Was This Post Helpful? 3
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 04:39 PM

View Postcfoley, on 14 June 2011 - 07:37 PM, said:

The most precise has to be BigDecimal, but it will also be the slowest option and the most awkward to code with. double is pretty precise and is very good for scientific and engineering calculations, but it suffers from rounding errors when converting it to decimal for display.

All Engineers will tell you that they perform their calculations with 10% accuracy.
After all Appolo 9 went to the moon with Slide Rule that have 2 to 3 digits or accuracy :)
Was This Post Helpful? 1
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 04:50 PM

My recommendation is double, unless you can get away with an integer type. I have an application that uses the int data type to store milli-Angstroms. The data files I use supply atomic coordinate data in Angstroms to three decimal places, and there is no need for me to store more decimal places.
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 04:57 PM

View Postcfoley, on 14 June 2011 - 07:50 PM, said:

My recommendation is double, unless you can get away with an integer type. I have an application that uses the int data type to store milli-Angstroms. The data files I use supply atomic coordinate data in Angstroms to three decimal places, and there is no need for me to store more decimal places.

int and long have 100% accuracy
if your data (even multiply by a long to take care of decimal digits) do not fit in a long you might have a design problem
Was This Post Helpful? 1
  • +
  • -

#11 R41D3NN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 14-June 11

Re: What is the Best Data Type For Accuracy?

Posted 14 June 2011 - 04:57 PM

View Postcfoley, on 14 June 2011 - 04:50 PM, said:

My recommendation is double, unless you can get away with an integer type. I have an application that uses the int data type to store milli-Angstroms. The data files I use supply atomic coordinate data in Angstroms to three decimal places, and there is no need for me to store more decimal places.


This is an excellent idea to use an integer type. Just as you have a data file to supply atomic coordinates, I could easily create a data file for my body locations.

This post has been edited by R41D3NN: 14 June 2011 - 04:59 PM

Was This Post Helpful? 1
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: What is the Best Data Type For Accuracy?

Posted 15 June 2011 - 12:25 AM

If you do go down the int or long route, a few conventions I use might be helpful. I'm sure you plan on having getX(), getY() methods. If you are working with funny units, it is a good idea to note the unit in the variable or method name. Especially if you switch between units. So, for my milli-Angstroms, I have these methods:

public final int getX_mA(){.....}
public final int getY_mA(){.....}
public final int getZ_mA(){.....}



I also have these:

public final double getX_A(){.....}
public final double getY_A(){.....}
public final double getZ_A(){.....}



Since the latter are almost exclusively used for display, I have these too:

public final String getXstr_A(){.....}
public final String getYstr_A(){.....}
public final String getZstr_A(){.....}



You could see the code duplication here and decide to implement a Scalar class, but I decided that was overkill for my purposes.
Was This Post Helpful? 1
  • +
  • -

#13 R41D3NN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 14-June 11

Re: What is the Best Data Type For Accuracy?

Posted 15 June 2011 - 07:54 AM

View Postcfoley, on 15 June 2011 - 12:25 AM, said:

If you do go down the int or long route, a few conventions I use might be helpful. I'm sure you plan on having getX(), getY() methods. If you are working with funny units, it is a good idea to note the unit in the variable or method name. Especially if you switch between units. So, for my milli-Angstroms, I have these methods:

public final int getX_mA(){.....}
public final int getY_mA(){.....}
public final int getZ_mA(){.....}



I also have these:

public final double getX_A(){.....}
public final double getY_A(){.....}
public final double getZ_A(){.....}



Since the latter are almost exclusively used for display, I have these too:

public final String getXstr_A(){.....}
public final String getYstr_A(){.....}
public final String getZstr_A(){.....}



You could see the code duplication here and decide to implement a Scalar class, but I decided that was overkill for my purposes.


Fantastic idea of embedding the unit in the method name, I have been researching different ways of implementing units but have really only found that I need to convert everything to its base-unit and then if needed to use units for whatever convention I might be using. Also, a scalar class is not such a bad idea and for the gist of things, it would be overkill to show here. ;)
Thanks
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: What is the Best Data Type For Accuracy?

Posted 15 June 2011 - 08:19 AM

No problem. Good luck!
Was This Post Helpful? 0
  • +
  • -

#15 R41D3NN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 14-June 11

Re: What is the Best Data Type For Accuracy?

Posted 17 June 2011 - 06:17 PM

So, BigDecimal it is. I was just practicing a bunch with that data-type by making a calculate pi program which I will post in a Advanced Programming forum to get feedback on my code. I find that this data-type isn't too terrible to work with, and the time to calculate isn't terrible... For my program though, I might want to have a option of which data-types in real-time and then also an option to compile a scene into an animation to use just the BigDecimal option for real-time playback for accuracy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1