Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange behavior with double variables
#1
Hi, what happens in this macro? It shows 'Amount=0.06' and the data type is double, so I don't understand what is going on.

Any help, please?


Macro WhatHappens
Code:
Copy      Help
str List

List=
;*** FirstLine ***
;John: sells
;Tony: sells
;Mike: pays1 $0.02
;Rose: sells
;Alex: sells
;Mary: pays1 $0.01
;Doug: sells2
;*** LastLine ***

int FirstLine LastLine

FirstLine=0
LastLine=8

double Amount

Amount=0.03

Calculate FirstLine LastLine List Amount
;Amount=0.06

out
out "Amount: %.5G" Amount

if Amount=0.06;; <<<<<<<<<< ERROR <<<<<<<<<<
,out "OK"
else out "ERROR!"

Function Calculate
Code:
Copy      Help
function int'FirstLine int'LastLine str'List double&Amount

int i pos offset
double paid AmountAux
str a s sline

AmountAux=Amount

for i FirstLine LastLine
,sline.getl(List i)
,pos=findrx(sline " pays1 | notinterested | outofservice " 0 0 a)
,if pos<>-1
,,s.left(sline pos-1)
,,sel a
,,,case " pays1 "         : offset=8
,,,case " notinterested " : offset=7
,,,case " outofservice "  : offset=5
,,s.get(sline pos+offset)
,,if s="0.02"
,,,paid=0.02
,,if s="0.01"
,,,paid=0.01
,,AmountAux+paid

Amount=AmountAux
#2
Floating point calculations are not completely precise. For example, 0.03+0.02+0.01 gives result 0.060000000000000005. You can use function Round() to make it 0.06.

Macro Macro1756
Code:
Copy      Help
double Amount

;Amount=0.06
Amount=0.03+0.02+0.01

out
out "Amount: %.5G" Amount
out "Amount: %.20G" Amount ;;0.060000000000000005

if Amount=0.06;; <<<<<<<<<< ERROR <<<<<<<<<<
,out "OK"
else out "ERROR!"

Amount=Round(Amount 2)

if Amount=0.06
,out "OK"
else out "ERROR!"

Same in other languages, eg VBScript.
Macro Macro1758
Code:
Copy      Help
str vbs=
;dim Amount
;Amount=0.03+0.02+0.01
;msgbox Amount
;if Amount=0.06 then
,;msgbox "OK"
;else msgbox "ERROR!"
;end if
VbsExec vbs
#3
Thanks for your help, Gintaras.

I think that you could improve the help file warning about this fact.

Other weird behavior is the order of the math operations:

QM: from left to right

Macro Macro181
Code:
Copy      Help
int a=2+3*3
out a

QM: 15
Usually:11

I spent hours trying to discover these problems XD

Anyway, QM rocks!
#4
Macro Macro2
Code:
Copy      Help
int a = 2+(3*3)
out a
#5
80)

looks like that one bit you too.
http://en.wikipedia.org/wiki/Order_of_operations
An old blog on QM coding and automation.

The Macro Hook
#6
Macro Macro126
Code:
Copy      Help
double n

n=3/2
out n;; n=1 ??

n=3.0/2
out n;; ok

n=3
n=n/2
out n

Why?
#7
In expression 3/2, types of both operands are int, therefore calculates the expression with int precision.
In expression 3.0/2, type of one of operands is double, therefore calculates the expression with double precision, because it is higher than int.
The rules are like in C++ language.


Forum Jump:


Users browsing this thread: 1 Guest(s)