### Math

Posted:

**Thu Feb 07, 2002 3:25 pm**For what to take care about, while doing math in NB.

A few days ago I was just wondering about an old PUB, created with NB 3.2. Running under 3.2, all was ok, running under 4.x all was wrong. What happened?

The PUB was a little code, calculating prime-numbers, that means doing lots of math calculations and a few IF comparisons.

After looking to the code, I could not find any logical error, especially it worked fine under 3.2.

After a few testings I got the reason.

In fact, NB 4.x treats ALL user-variables as string, only in case of math expressions, there will be a type-conversion. But this will be done at runtime by the NB-runtime each time the interpreter will find some formulas. But the variables, especially the results, are always treated and stored as string.

Normally, the "." is the decimal-separator doing calculations. NB accepts both, "." and ","

In math logic, 2.00 is the same than 2.000 or 2 NOT so NB

in NB, 2,00 is the same for calculation than 2.00 or 2, but all numbers are handled as string. That´s very important to know. Why?

When using the math statement to do some calculations, there is one parameter, specifying the decimals of the result.

Math "5/2" "4" "[Result]" will store 2,5000 to [Result] and NOT 2.5

Math "5/2" "2" "[Result]" will store 2,50 to [Result] and NOT 2.5

and so on. The mentioned type conversion will be done while using the MATH command, BUT NOT while using the IF command.

In case you want to compare a calculated variable this is very important !!!!

Math "5/5" "4" "[Result]"

if "[Result]" "=" "1"

playtone "440"

endif

Will NOT work, nevertheless the logic AND the code is correct.

Math "5/5" "4" "[Result]"

if "[Result]" "=" "1.0000"

playtone "440"

endif

Will NOT work, because of the "." as decimal separator

Math "5/5" "4" "[Result]"

if "[Result]" "=" "1,0000"

playtone "440"

endif

Now works fine, because [Result] contains "1,0000" as STRING

So you have to take very special care for the number of decimals, you are using inside a math formula.

In case you know this behaviour of NB, it is not so much serious, it is only a bit other than normally handled by standard languages like Delphi or C.

Rgds

Gerry

A few days ago I was just wondering about an old PUB, created with NB 3.2. Running under 3.2, all was ok, running under 4.x all was wrong. What happened?

The PUB was a little code, calculating prime-numbers, that means doing lots of math calculations and a few IF comparisons.

After looking to the code, I could not find any logical error, especially it worked fine under 3.2.

After a few testings I got the reason.

In fact, NB 4.x treats ALL user-variables as string, only in case of math expressions, there will be a type-conversion. But this will be done at runtime by the NB-runtime each time the interpreter will find some formulas. But the variables, especially the results, are always treated and stored as string.

Normally, the "." is the decimal-separator doing calculations. NB accepts both, "." and ","

In math logic, 2.00 is the same than 2.000 or 2 NOT so NB

in NB, 2,00 is the same for calculation than 2.00 or 2, but all numbers are handled as string. That´s very important to know. Why?

When using the math statement to do some calculations, there is one parameter, specifying the decimals of the result.

Math "5/2" "4" "[Result]" will store 2,5000 to [Result] and NOT 2.5

Math "5/2" "2" "[Result]" will store 2,50 to [Result] and NOT 2.5

and so on. The mentioned type conversion will be done while using the MATH command, BUT NOT while using the IF command.

In case you want to compare a calculated variable this is very important !!!!

Math "5/5" "4" "[Result]"

if "[Result]" "=" "1"

playtone "440"

endif

Will NOT work, nevertheless the logic AND the code is correct.

Math "5/5" "4" "[Result]"

if "[Result]" "=" "1.0000"

playtone "440"

endif

Will NOT work, because of the "." as decimal separator

Math "5/5" "4" "[Result]"

if "[Result]" "=" "1,0000"

playtone "440"

endif

Now works fine, because [Result] contains "1,0000" as STRING

So you have to take very special care for the number of decimals, you are using inside a math formula.

In case you know this behaviour of NB, it is not so much serious, it is only a bit other than normally handled by standard languages like Delphi or C.

Rgds

Gerry