(41) Jet fuel Density calculator - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP-41C Software Library (/forum-11.html) +--- Thread: (41) Jet fuel Density calculator (/thread-18068.html) |
(41) Jet fuel Density calculator - WrongWay - 02-23-2022 05:38 AM The attached program computes the fuel density of JET A fuel and compute the needed volumetric fuel uplift. This is my first time back to the 41 after a fair bit so the code I'm sure could easily be optimized and constructive feedback is always welcome. The compressed raw file is attached. XEQ FDENSTY to run. and follow on screen instructions. The initial prompt asks for an entry of 0 or 1 to set the desired units. size[ 00E4 ] ( 228 bytes ) Code:
RE: (41) Jet fuel Density calculator - Thomas Klemm - 06-25-2022 03:47 AM (02-23-2022 05:38 AM)WrongWay Wrote: … the code I'm sure could easily be optimized and constructive feedback is always welcome. if-else Statements In general an if-else statement like this: Code: if predicate: … can be translated into: Code: predicate? This uses only two labels instead of three: Code: 33 FS? 00 Since we use only forward jumps, both labels can be reused in other occurrences as well: Code: 52 FS?C 00 This also avoids using the expensive global labels "K" and "L". Polynomial Evaluation A quadratic polynomial can be rewritten using Horner's method: \( \begin{align} ax^2 + bx + c = (ax + b)x + c \end{align} \) This avoids calculating powers of \(x\). If we first fill the stack with \(x\) it will be copied from register T down the stack with each operation. Since the coefficients are not used elsewhere we don't have to store them in registers: Code: 12 ENTER Saving Results I decided not to save the density in a register but leave the result on the stack for further computations: Code: 43 "FPL FUEL?" Of course the user might mess with the stack which may lead to wrong results. This leaves us with the following program for the HP-42S: Code: 00 { 191-Byte Prgm } It should also work with the HP-41C after the obvious transformations. We end up with a program with 20 fewer lines, using 191 bytes instead of 230, and using no registers. RE: (41) Jet fuel Density calculator - WrongWay - 05-12-2024 09:36 PM Thank you for the awesome feedback and excellent explanation. |