Re: why multiplying with a large number gives negative and strange answers

From: Dave Allured - NOAA Affiliate <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Fri Dec 20 2013 - 18:39:29 MST

Rashed,

This is a subtle problem. You are getting integer overflow, but NCL does
not issue a warning message in this case.

In this line: an2=a*1000000000000

The number 1000000000000 is created as a default 32-bit signed integer
because there is no suffix. Please see the manual page on how to declare
numeric constants:

http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclDataTypes.shtml#BasicNumericTypes

However, that number is too large to fit within a default 32-bit integer.
 Overflow occurs during translation of this line of code, high order bits
are chopped off without warning, and the result is seemingly a random
number.

You can get this line to work correctly by changing to a data type with
more range capacity. Add a suffix or decimal point to change the data
type, such as any of the following:

    an2=a*1000000000000. ; multiply by single precision float
    an2=a*1000000000000d ; multiply by double precision float
    an2=a*1000000000000q ; multiply by 64-bit integer

Please reply to user list only.

--Dave

On Fri, Dec 20, 2013 at 5:24 PM, Rashed Mahmood <rashidcomsis@gmail.com>wrote:

> Hi
>
> I am having simple but strange problem when multiplying a number with
> large number:
> Here is simple example: Please check the an2 value.
>
> ncl 0> a=0.01
> ncl 1> an=a*1000000000
> ncl 2> print(an)
>
> Variable: an
> Type: float
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) 1e+07
>
> ncl 3> an2=a*1000000000000
> ncl 4> print(an2)
>
>
> Variable: an2
> Type: float
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) -7273800
>
> I am not sure why an2 is -7273800? (I am using NCL 6.1.2)
>
> Regards
> Rashed
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Dec 20 18:39:40 2013

This archive was generated by hypermail 2.1.8 : Mon Jan 06 2014 - 13:02:22 MST