Hello everyone,
I'm currently making a program to calculate the value of pi, I believe using Simpson's Rule. I'm very new to the math and am still trying to get everything straight in my head, but I understand enough that I can start to work on some of the programming logistics of it. I'm basing a lot of this off of information from here.

Anyways, right now I'm essentially trying to divide 1 into a given number of divisions. The user is actually putting in a number of data points that will be created by dividing 1 by a certain number, which is the number of data points minus 1. For example, in order to get 5 data points, you would have to divide 1 by 4. You'd get 0, .25, .5, .75, and 1 as data points. I'm storing these data points in an array. I think I will create a separate value for the number of divisions as to avoid even more math when assigning the values of the data points to variables in the array. I'm getting these values through a for loop. The basic idea is something like this:

Now this would be all fine and dandy if it weren't for the fact that I want to use a float, not an int. I've never really worked with floats, but I'm pretty sure you have to tag an "f" on to the end of the number you are assigning it. I guess my main question is this: how do you assign a float type variable a value based off of another variable? I feel kind of stupid for asking it, but I guess I've just never really worked with floating point numbers in Java '~'. Thanks in advance!

your code is confusing. You define an int array, but then try and assign the results of a division to it - you realize that your results will all be rounded/truncated (I"m not sure which)?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63

posted

0

Yeah, sorry, I suppose it wasn't clear. The only reason I put the variable type as int was because I wasn't sure what having an array of floats would look like. I don't want to use a double because I could be working with a really big number with a lot of decimal places, and that would eat up a lot of memory. Do you sort of understand what I was saying? It's hard to communicate these things in just forum posts. At least for me T_T.

I'm not sure I do understand. A float will always be 32 bits, regardless of whether you are storing 0 or 6.023 x 10^23. An array of floats will always be (32 bits) x number of elements + a little overhead, again regardless of the values stored in those floats.

doubles will always be 64 bits.

there is a limit to how many decimal places a float or a double can hold, and there are discrete values. If you try and assign some value in between, your data will be rounded off to the closest.

Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63

posted

0

Where can I find out how many decimal places a float and double can hold? I tried Googling it, but not much came up. Also, I think I saw something about putting the letter f after the number when you are assigning a value to a float, and I'm not sure how to put that in when you are doing math. For example:

it's not a question of how many decimal places...as the number gets large, the answer is none, and as it gets close to zero, the answer is a lot.

both floats and doubles are stored as two parts - the mantissa and the exponent. Some of the bits hold the precision, and some hold the exponent (think scientific notation).

So, as the number gets very big, you may have 10 (don't quote me on that) digits of precision, then a whole lot of zeros, then a decimal point with NO decimal places after it.

OR, if the number is close to zero, you may have a decimal point, a whole lot of zeros, and then 10 digits - but there are still discrete values it can hold.

To force the results, you can make your literal a float:

float blah = i/24.0;

or if you are using two integer terms, you can cast one to a float

int a = 3;
int b = 7
float c = a/ (float)b

subject: Working with floating point numbers in for loops and arrays