• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Circumventing Integer Division

    March 16, 2011 Hey, Ted

    My computer seems to have forgotten how to divide. When I divide one value by another, using SQL, the answer is always exactly one or zero. What gives?

    –RPG Professional

    This took a little while, but we finally found his problem. First, the background.

    Assume a database file with three fields: customer ID, the number of orders from that customer, and the number of shipments to the customer.

    select CustID, Orders, Shipments
      from Summary  
              
    CUSTID  ORDERS       SHIPMENTS
         1      75              25
         2      10              16
         3      20               0
         4       8               8
    

    Now let’s add a ratio to the query to gauge order fulfillment.

    select CustID, Orders, Shipments, 
           Shipments / Orders as Ratio
      from Summary              
    CUSTID  ORDERS       SHIPMENTS           RATIO
         1      75              25               0
         2      10              16               1
         3      20               0               0
         4       8               8               1
    

    The problem, as it turned out, was the orders and shipments fields were defined as integer. The programmer had used SQL to create the summary table on the fly, and the system had inferred that the two fields would always contain whole numbers.

    Since the two fields were integer fields, SQL used integer division. According to the SQL reference:

    If both operands of an arithmetic operator are integers with zero scale, the operation is performed in binary, and the result is a large integer unless either (or both) operand is a big integer, in which case the result is a big integer. Any remainder of division is lost.

    To get a result with decimal positions, I had him use the DOUBLE function over the two fields. Since both operands were double precision, the system carried out division of real numbers. To get a readable result, I had him use the DEC function.

    select CustID, Orders, Shipments,
           dec(double(Shipments) / double(Orders),4,3) as Ratio
      from Summary
    
    CUSTID          ORDERS       SHIPMENTS   RATIO
         1              75              25    .333
         2              10              16   1.600
         3              20               0    .000
         4               8               8   1.000
    

    RPG also supports integer division, if you want it. Use the %DIV built-in function.

    eval Ratio = %div(Shipments: Orders);
    

    Integer division has its purposes, but for business computing, I have rarely used it.

    –Ted



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    SEQUEL Software:  FREE Webinar: Overcoming query limits with SEQUEL. March 23
    SkyView Partners:  The IBM i security compliance provider who does all the heavy lifting
    COMMON:  Join us at the 2011 Conference & Expo, May 1 - 4 in Minneapolis, MN

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    VAI Completes Dual ERP Integration for Two Distribution Companies Raz-Lee Feeds IBM i Data into RSA SIEM

    Leave a Reply Cancel reply

Volume 11, Number 9 -- March 16, 2011
THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
Botz & Associates, Inc.

Table of Contents

  • Duplicating CPYF Function Using SQL
  • Circumventing Integer Division
  • Admin Alert: Corralling i/OS Storage Hogs, Part 2

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle