Simple calculations ; wrong Syntax or ...

... i have again to start reading the book from page 1 :blush:


Dear Community,

Was looking through the reference and many example files, but did not get the solution for my problem. - I want to display several values on a TFT display, to test the code and look i want to use random numbers. Fortunately the "hard" part, displaying the data, works fine but i got stuck completely with the Syntax or his sister or her brother (however ...) for simple calculations ...

1.)
This was the 1st attempt which seemed to me obvious to work :

val = int((random(maxval) / maxval) * 100);

This expression gives always 0 ... do i want to do things which are impossible or am i using the impossible way ?

2.)
I tried now to break up the math with single line calculations and display the calculation progress in the serial monitor. To show you what i see i made a screendshot of the code and the serial datagram (val, rndnum = int ; valcalc1, valcalc2 = float ; maxval = 1000) :

Maybe i have to sleep some more hours and i will see the issue, my answer is still 42, but this doesnt help :. ... maybe you know the answer ? - Any hint is welcome !

val = int((random(maxval) / maxval) * 100);

Anything times zero is zero - that's not syntax, that's arithmetic.

hmm

I think you need to use float here:

int maxval = 1000; //whatever you want maxval to be
int rndnum = random(maxval);
float valcal1 = rndnum/maxval;
float valcal2 = valcal1 * 100;
int val = int(valcal2); //assuming int() is a function that rounds valcal2 to the nearest integer...

AWOL:

val = int((random(maxval) / maxval) * 100);

Anything times zero is zero - that's not syntax, that's arithmetic.

Well, i'm aware of that fact, but for my understanding the result of the term "random(maxval)" is a random value with its maximum at "maxval". The term "(random(maxval) / maxval)" could be zero but will be almost a something between 0 and 1 ... thats why i think of a boring syntax error ...

@Qdeathstar:
That's exactly how i defined all of the var's

float valcal1 = rndnum/maxval;

No.

float valcal1 = (float)rndnum/(float)maxval;

int(n) truncates to largest integer <= n

you can combine all "constant" variables - especially if maxval is defined const, a max value typically is constant or?

const int maxval = 1000;
float rndnum = random(maxval) * (100.0/maxval);  // combine the constants so the compiler can optimize.
int val = round(rndnum ); // rounding iso truncating?

Looking at the semantics of the code, you seem to want a random integer between 0..100 (percentage?)
is that right?

Thx Mike, that really helped me now, it works ! - [/SyntaxError]


@robtillaart :
Yes, i need a percentage value as an int and maxval is a constant. - Many thanks for your proposal, i think thats the way how i wanted it myself :wink:

The thing you have to realise is that integer-only division is not division, because
fractions cannot be represented as integers. The relation

(a / b) * b == a

Simply does not and cannot hold when b is not 1, when using integer arithmetic.

Many computer languages support only integer and floating point arithmetic,
so fractions have to be represented as floating point.

In integer arithmetic the act of division a/b yields a quotient q and a remainder r
which obey the relations:

q *b + r = a, 0 <= r < |b|

(Except that in C this doesn't hold for negative numbers because the inventers
of the language failed maths AFAICT!)

In C % is the remainder operator, aka modulo operator.