comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: IDL mathematics
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: IDL mathematics [message #2703] Wed, 31 August 1994 13:07 Go to next message
velt is currently offline  velt
Messages: 19
Registered: June 1994
Junior Member
>
> How come the following expression gives the wrong answer.
>
> J = 201+((1461*(1994+4799))/4)-(3*((1994+4899)/100)/4)-2465022
>
> J = -2457713
>
> It should be:
>
> J = 201+((1461.*(1994+4799.))/4.)-(3.*((1994+4899.)/100.)/4.)-24 65022.
>
> J = 16270.5
>
>
> Kelly Dean


Most of your calculations are done in 2-byte integer, upto the last
subtraction. The intermediate results are overflowing, so you get
the wrong answer. Throw in a few longs at strategic places:

J = 201+((1461*(1994L+4799))/4)-(3*((1994L+4899)/100)/4)-2465022 print,J
--> 16271

Robert Velthuizen,
Digital Medical Imaging Program of the
H. Lee Moffitt Cancer Center and Research Institute at the
University of South Florida.
Re: IDL mathematics [message #2704 is a reply to message #2703] Wed, 31 August 1994 13:20 Go to previous message
paul is currently offline  paul
Messages: 22
Registered: June 1991
Junior Member
In article <342fs2$8sg@yuma.ACNS.ColoState.EDU>, Kelly Dean writes:
>
> How come the following expression gives the wrong answer.
>
> J = 201+((1461*(1994+4799))/4)-(3*((1994+4899)/100)/4)-2465022
>
> J = -2457713
>
The problem is with the large vlaue of 1461*(1994+4799), which overflows
one could do

IDL> print, 201+((LONG(1461)*(1994+4799))/4-3*((1994+4899)/100)/4)-24650 22
IDL> 16271

note that

IDL> print, 1461.*(1994+4799)
9.92457e+06

is like

IDL> print, fix( 9.92457E06)
28634

and not like

IDL> print, long( 9.92457E06)
9924570


Using LONG rather than converting to floating point preserves any integer
truncation properties that may (or may not) be desired.

Paul Schopf
Oceans and Ice Branch
NASA GSFC
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: More Q's on TV and Postscript
Next Topic: More Q's on TV and Postscript

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 21:16:54 PDT 2025

Total time taken to generate the page: 0.09910 seconds