excel - Why does math operation on large numbers sometimes need an intermediary variable to return a correct result? -


i have been using excel 2013 here. example,

public function roundtest(byval flnumber double) double   roundtest = round(flnumber) end function  public function test( _     byval flnumber double, _     byval fldivisor double) _     double    test = flnumber - (roundtest(flnumber / fldivisor) * fldivisor) end function 

let fldivisor passed 10 caller. calling test() flnumber <= 10^22 yields correct result, 0, flnumber > 10^22, test() returns wrong result, negative number. however, if intermediary variable used temporarily hold partial result of calculation, test() returns correct result, 0.

public function test( _     byval flnumber double, _     byval fldivisor double) _     double   dim fltemp double    fltemp = roundtest(flnumber / fldivisor) * fldivisor   test = flnumber - fltemp end function 

why happens? how can avoid peculiarity?

here more straightforward example:

public function roundtest(byval flnumber double) double     roundtest = round(flnumber) end function  sub testcdbl()     dim double     dim b double     = 10 ^ 23     b = 10     w1 = - roundtest(a / b) * b ' -8388608     w2 = - round(a / b) * b ' 0     w3 = - cdbl(roundtest(a / b) * b) ' 0  end sub 

imo intrinsic implementation of calculations works results of native round() function , returned roundtest() function processed in different ways. turns out may process part of expression , particularly subtrahend not double type, , having explicit conversion double cdbl() might in case, instead of coercion assigning temp variable of double type.
you have bear in mind effort , suchlike not guarantee fix issue. each calculation can introduce floating point errors, @comintern commented.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -