Skip to main content

Please Help Me I Appreciate

4 replies [Last post]
javamon987654321
Offline
Joined: 2012-05-07
Points: 0

I am doing a Java program on sequences for my computer studies project.
The linear sequences are sequences that have the same difference example 2, 4, 6, 8, 10 (a difference of 2) or 55, 66, 77, 88, 99(a difference of 11). To find out the difference my program gets the last number and reduces the number before it (ex: 99 - 88=11, 88 - 77=11, 77 - 66=11). If the differences obtained are not equal it means that it is not a linear sequence. My problem is that when I enter a sequence with a decimal point like this 5.5, 6.6, 7.7, 8.8, 9.9 (so it has a difference of 1.1) it tells me that it is not a linear sequence. Can you tell me what`s the problem please. BTW I am using jcreator.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
teleporter
Offline
Joined: 2012-05-14
Points: 0

You should attached some codes for us to help you.

javamon987654321
Offline
Joined: 2012-05-07
Points: 0

Thanks a lot but I fiqured it out. I made the program to print the difference to see what was wrong and it wasn`t printing the difference of 1.1, but for example 1.1000000000005 and the next difference would be a different number and wouldn`t match. So know I have to find a code to round the number

grelf
Offline
Joined: 2007-11-14
Points: 0

And of course the cause is that numbers are stored in memory in binary. Even a simple decimal like 0.1 is a recurring number in binary. It is impossible to store exact equivalents of all decimal fractions. You must either round numbers of type float or double, or use java.math.BigDecimal (which stores each decimal digit as a binary number and so is exact - especially important for currency calculations).

javamon987654321
Offline
Joined: 2012-05-07
Points: 0

10ks a lot mate.