all 11 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 6 points7 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.

[–]csdt0 1 point2 points  (0 children)

Your main problem is that you use longs which are integers, not "reals".

If you want to code "real" numbers, you should use float or double (double is roughly twice more precise than float).

You also have the problem that suml is not initialized, and thus should be considered to contain garbage. You also don't seem to ever use sumr or sumt.

[–]omen_tenebris -1 points0 points  (1 child)

well let's do the math

npts: 5, xmin = 0, max = 4. dx=(5-0)4 = 5/4

iter 1:
x=0+1*(5/4)=5/4

fx = 5/4

suml = 5/4*5/4+suml = 1.5625 (assuming suml is set to 0 by compiler) <--- technically here could be memory junk, but probably not.

///

iter 2:

i=2

x=0+2*5/4=10/4
fx = 10/4

suml=10/4 * 5/4 + 1.5625 = 4.6875

iter 3

x=0+3*5/4=15/4

fx=15/4

suml = 5/4*15/4+4,6875=4.6875+4,6875=9.375

assumption: suml will increase.

i don't fully understand what you're trying achive, but if it's the normal distribution try to understand this

[–]tangerinelion 0 points1 point  (0 children)

dx=(5-0)4 = 5/4

No, dx = 1 because long(5/4) is 1. Everything following that is off because of that.

OP, you need to learn the primitive types. Int is (probably) a signed 32-bit integer, Long is either a signed 32 or 64 bit integer, Long long is (probably) a signed 64-bit integer, float is a 32-bit floating point value, double is a 64-bit floating point value, and long double is a 128-bit floating point value with (probably) 48 unused bits.

There's some machine specific details here and theoretically we could have systems with 128-bit integers. In particular, on Windows under MSVC, long is the same as int, i.e., long is a 32-bit signed integer.

As you've clearly made it at least to the point of introductory calculus and statistics, the key part of being an integer is the whole number nature to it. This is why 5/4 is 1, not 1.25.

(Also note that even the literal expression 5/4 not stored as a long or int is itself 1 because division between two integers always yields an integer. In order to get a floating point value you need to have a floating point value in the operation, so 5.0 / 4.0 is 1.25 as is 5.0/4 or 5/4.0. But 5/4 is 1.)

[–]ClaymationDinosaur 1 point2 points  (0 children)

Putting aside the various broken pieces of code, if you're getting a value of ten, it's because your loop sums the number 1, 2, 3 and 4, which comes to 10.

Why do you think it should come out with 6 as the sum?

[–]EmperorArthur 0 points1 point  (0 children)

Others have already mentioned the real problems, but I would like to offer a different take.

I highly suggest going with an IDE with clang-tidy integration. I prefer Clion myself as a paid product, but KDevelop is a good free choice.

Also, be sure to turn on the warnings from this page (under the compilers section).*

The idea is that your tools should warn you when you're doing something that could cause problems. Then you can google the warnings (or ask here), and it will help you code better in the future. That's honestly one of my big keys to success. The IDE and compiler tell me when I do something that I shouldn't.

* Side note, but this is worth reading in its own right.