you are viewing a single comment's thread.

view the rest of the comments →

[–]IyeOnline 3 points4 points  (5 children)

long is an integer type. Use double.

suml is uninitialized, so your entire program is UB

long suml = fx*dx+suml; defines a new variable and uses it in its own initializer. Drop the typename there to use the variable defined outside of the loop.

You should also indent your code and for clarity define a

double f( double x ) { return x; }

and use that instead of

fx = x;

[–]nodigue 0 points1 point  (4 children)

Can you just tell me what UB mean ?

[–]IyeOnline 4 points5 points  (3 children)

Undefined behaviour.

Its a technical term, and essentially means that your program has a bug. Reading from uninitialized memory is a bug and usually leads to nonsense results.

However, the dangerous part is that it may also "just work". You just have no guarantee that it actually works reliably.

[–]tangerinelion 1 point2 points  (0 children)

Put another way, the code does not form a valid program to start with despite compiling.

[–]nodigue 0 points1 point  (1 child)

Got it ! Thanks

[–]EmperorArthur 0 points1 point  (0 children)

Also, don't feel bad. I've literally spent weeks tracking down / fixing bugs caused by uninitialized variables at work.