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
Joined: Mar 28, 2008
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.
Joined: Mar 28, 2008
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: