From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,791ecb084fdaba75,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-09-27 16:03:47 PST Newsgroups: comp.lang.ada Path: bga.com!news.sprintlink.net!howland.reston.ans.net!swrinde!news.dell.com!tadpole.com!uunet!clsi!graham From: graham@clsi.COM (Paul Graham) Subject: Types with physical dimension Message-ID: Sender: usenet@clsi.COM Organization: COMPASS Design Automation Date: 27 Sep 1994 22:18:41 GMT Date: 1994-09-27T22:18:41+00:00 List-Id: While Ada's strong typing mechanism can catch errors like this: d : dollars; f : francs; ... d := d + f; -- error it does allow such nonsensical constructs as d := d * d; -- what does it mean to multiply dollars by dollars? d := d ** 10; -- ?? VHDL provides physical types to model values with physical dimensions. A VHDL physical type lacks certain predefined multiplication operations, lacks exponentiation, and its division operation returns a universal_integer (or root_integer in Ada 9x terminolgy). An example of physical types: type length is range integer'low to integer'high units inches; feet = 12 inches; yards = 3 feet; end units; type area is range integer'low to integer'high units inches_2; feet_2 = 144 inches_2; yards_2 = 9 feet_2; end units; function "*"(x, y : length) return area is begin return length'pos(x) * length'pos(y) * inches_2; end; function "/"(x : area; y : length) return length is begin return area'pos(x) / length'pos(y) * inches; end; ... variable L : length; variable A : area; ... L := L * 2; -- legal L := L * L; -- illegal A := L * L; -- legal A := 2 inches * 2 inches; -- legal (= 4 inches_2) L := 4 feet_2 / 2 inches; -- legal (= 288 inches) (Please excuse any typos; I haven't simulated this.) One can model physical types in Ada by using private types, but this involves writing a lot of operators. What do Ada programmers do to guard against dimensional errors? Paul -- Paul Graham graham@compass-da.com Compass Design Automation, Inc. (speaking only for myself) "Cekoslovakyalilastiramadiklarimizdanmissiniz."