S
SB
Guest
On Feb 19, 11:41 pm, sh...@cadence.com wrote:
I revisited the numbers and I got the TB to $display them with a
much greater accuracy (%20.18f). It seems that the number that I see
as
3334.40 is in fact slightly different at 3334.3999987.... I had not
anticipated this at the time and as such accuracy is lost in the
calculation.
Now I know why the $rtoi gives a value of 79, as it is merely
truncating
the 79.99999 that is produced by dividing the 33334.3999987... figure
by 41.68.
As regards your last paragraph, I have learned much re the rounding
errors to which floating point number calculations are prone. I will
also
note the truncating effect of the $rtoi function. I will try to
operate in integers instead or a try new approach entirely. I am
also considering using the fudge factor method that you speak of as an
expected margin of error.
Thank you for your time and assistance on this.
Regards
SB.
Thank you for this detailed analysis.If I use Stephen's test program, NC-Verilog also gets a result of 80.
Changing it to print all the real values with %20.18f formats to see
them with more precision, I get
3334.400000000000090949 / 41.679999999999999716 > 80.000000000000000000
So despite the roundoff error in the closest representable values to
3334.4 and 41.68, the value comes out fine.
One of your values must have more than this minimum possible roundoff
error.
For the moment however, the 3334.40 figure ultimately
comes from a simulation timestamp using the
$realtime command.
It is possible that the conversion of the time to a real in $realtime
is accumulating some roundoff error. However, a test with a timescale
of 10ns/1ns and a delay of #3334.4 gives me the exact same value as
above.
The 41.68 figure comes from the mulitplication
of an 8bit value by a real value. Hence, the
problem may arise here. I may have to change this
8bit value to a real quantity. I will follow
up on this.
It is quite possible that this calculation is causing the additional
roundoff.
(Is this a likely cause of the problem?)
Changing the 8bit value to a real value is unlikely to help. Small
integer values can be represented exactly in floating point, so that
is not where the roundoff error is coming from.
Ultimately, you need to recognize that floating point representation
and calculation are subject to roundoff error. You should not expect
to get the exact values that you can get with integers. You should
switch to integer calculations, or adjust for possible roundoff
error. For example, you might want to try rounding instead of
truncating. An implicit conversion from real to integer will round
instead of the truncation that $rtoi does. Or you can add 0.5 or a
small fudge factor before calling $rtoi.
I revisited the numbers and I got the TB to $display them with a
much greater accuracy (%20.18f). It seems that the number that I see
as
3334.40 is in fact slightly different at 3334.3999987.... I had not
anticipated this at the time and as such accuracy is lost in the
calculation.
Now I know why the $rtoi gives a value of 79, as it is merely
truncating
the 79.99999 that is produced by dividing the 33334.3999987... figure
by 41.68.
As regards your last paragraph, I have learned much re the rounding
errors to which floating point number calculations are prone. I will
also
note the truncating effect of the $rtoi function. I will try to
operate in integers instead or a try new approach entirely. I am
also considering using the fudge factor method that you speak of as an
expected margin of error.
Thank you for your time and assistance on this.
Regards
SB.