-2

I am recording data every 0.02 seconds and my code to save this information to a tab deliminated file is producing some peculiar results where even though i am adding 0.02 on each time to the time variable it starts being slightly off (i'm aware that when rounded it is essentially the same but it bugs me). How do i get it to look like the corrected data set

output.println(time + "\t" + accelX + "\t" + accelY + "\t" + accelZ);
time = time + 0.02;

the output gives me (I left out the other data as it is going in fine)

Time 0.02 0.04 0.06 0.08 0.099999994 0.1199999999

should be

Time 0.02 0.04 0.06 0.08 0.1 0.12   
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
D Mason
  • 86
  • 4
  • 17
  • 4
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – cdhowie Oct 11 '14 at 21:11
  • 5
    Why have you tagged your question `javascript`? Java and JavaScript are **very** different languages, running in **very** different environments. – T.J. Crowder Oct 11 '14 at 21:14
  • 1
    @T.J.Crowder OP is using Processing, which was originally a Java framework, but has since been ported to JavaScript as well. That doesn't justify the tag, but I'd guess that's why it's there. – kevinsa5 Oct 12 '14 at 15:47
  • This is why it is there :) little bit confused as to which i was actually using – D Mason Oct 12 '14 at 20:25

1 Answers1

1

First and foremost, don't use float where double will work. Next of all, use printf instead of println, and specify your output's significant digits.

e.g. something like this,

output.printf("%5.2f %5.2f %5.2f %5.2f", time, accelX, accelY, accelZ);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373