Creating spirals

I was going through the following code to create spirals in a book and saw that the float and double produced different results .While “double” produced a single stable spiral ,"float " went haywire to produced a ton of odd looking spirals. The author gave the reason that float values are computed with numerical inaccurately.Can someone give me a more insight on this , why are float values computed inaccurately?

Here is the code:
Header file : :

#include "ofMain.h"

class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();

double a, b;			//Angle and its increment   (Stable curves)
//float a, b;			//Angle and its increment	(Unstable curves)

ofPoint pos, lastPos;	//Current and last drawing position
ofColor color;			//Drawing color
int colorStep;			//Counter for color changing
ofFbo fbo;				//Drawing buffer

void draw1();			//Draw one line segment

void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
};

cpp file:

     #include "testApp.h"


 //--------------------------------------------------------------
void testApp::setup(){

ofSetFrameRate( 60 );	
fbo.allocate( ofGetWidth(), ofGetHeight() );

fbo.begin();
ofBackground( 255, 255, 255 );
fbo.end();

a = 0;
b = 0;
pos = ofPoint( ofGetWidth() / 2, ofGetHeight() / 2 );	//Screen center
colorStep = 0;
}

//--------------------------------------------------------------
void testApp::update(){

fbo.begin();		
for ( int i=0; i<200; i++ ) {
	draw1();
}
fbo.end();			
}

//--------------------------------------------------------------
  void testApp::draw(){
ofBackground( 255, 255, 255 );		
ofSetColor( 255, 255, 255 );
fbo.draw( 0, 0 );
}

//--------------------------------------------------------------
 //Draw one line segment
 void testApp::draw1(){

a += b * DEG_TO_RAD;	
b = b + 0.5;

lastPos = pos;			/
ofPoint d = ofPoint( sin( a ), cos( a ) );
float len = 10;
pos += d * len;

if ( colorStep % 100 == 0 ) {
	color = ofColor( ofRandom( 0, 255 ), ofRandom( 0, 255 ), ofRandom( 0, 255 ) );
}
colorStep++;

//Draw line segment
ofSetColor( color );
ofLine( lastPos, pos );        
}

Thanks!

I would do a little googling on something like “floating point precision” or “float versus double” and see what you can learn. It’s a little tricky to know exactly what you mean by unstable, but my intuition says it’s related to this. My guess is that the spiral with floats looks a bit jagged and the line segments don’t match all the time?

What’s the code in draw1()?

Hey, I havent dug into your code too much but this is a general issue when it comes to floating point numbers (numbers with decimal places). It’s quite a technical area of computer science, the “double” type is a double precision floating point number, and as it name implies it can basically store twice as much detail in the floating point part of the number.

What is happening is a float only has limited precision, so it is only accurate to a few decimal places. The more calculations you do, the more errors due to lack of precision sneak in. Since DEG_TO_RAD is a relatively small number (say 3/180) it needs a lot of decimal places to be accurate.

This guy has some good (detailed) information on floats


whenever i googled I end up seeing complex mathematical philosophy on chaos theory (my bad luck )

Sorry I didn’t post pictures. By unstable i didn’t mean jaggies , i meant the spiral itself getting unstable and drawn at various places in the of app .You could have got it by just testing the code .Here are the pictures:
Using

    double a,b;

float a,b

@pants almost exactly what i wanted ! Thanks

Ah, well that’s really interesting. Didn’t realize the code panes we’re scrollable here, haha. Now it makes a lot more sense.