(12C) Signum Function - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (12C) Signum Function (/thread-12529.html) Pages: 1 2 (12C) Signum Function - Gamo - 03-01-2019 06:29 AM Signum Function is an odd mathematical function that extracts the sign of a real number. In mathematical expressions the sign function is often represented as sgn. The Signum Function of a real number (x) is defined as follows: sgn(x) = -1 if x < 0 sgn(x) = 0 if x = 0 sgn(x) = 1 if x > 0 For more detail on this topic https://en.wikipedia.org/wiki/Sign_function ------------------------------------------------- Example: 123 [R/S] display 1 -23.33 [R/S] display -1 0 [R/S] display 0 ------------------------------------------------- Program: for HP-12C Code:  X=0 GTO 00 ENTER ENTER x √x ÷ Program: for HP-12C Platinum on RPN mode Code:  X=0 GTO 000 ENTER x² √x ÷ Gamo RE: (12C) Signum Function - Gerson W. Barbosa - 03-02-2019 03:00 AM Same number of steps (on the HP-12C), working on full range: Code:  01 ENTER  02 CHS 03 x<=y 04 CHS 05 x=0 06 n! 07 / RE: (12C) Signum Function - Gamo - 03-02-2019 05:47 AM For HP-12 Platinum on ALG mode Program: Code:  [X=0] [GTO] 000 1 [x] [X<>Y] [x²] [√x] [÷] [LSTx] [=] Gamo RE: (12C) Signum Function - Dieter - 03-02-2019 05:22 PM (03-02-2019 05:47 AM)Gamo Wrote:  For HP-12 Platinum on ALG mode I do not have a 12C Platinum or an emulator, so I cannot check this, but on a TI calculator with AOS I'd simply type [÷] [x²] [√x] [=] Does this also work on the 12CP ? Dieter RE: (12C) Signum Function - rprosperi - 03-02-2019 08:57 PM (03-02-2019 05:22 PM)Dieter Wrote:  I do not have a 12C Platinum or an emulator, so I cannot check this, but on a TI calculator with AOS I'd simply type [÷] [x²] [√x] [=] Does this also work on the 12CP ? Dieter But... if the input is zero, you get an error instead of zero output. RE: (12C) Signum Function - Gamo - 03-03-2019 02:31 AM Unique HP-12C Platinum in ALG mode I'm not sure if TI Calculator with AOS got the [LSTx] function. For 12CP in order to use [LSTx] it need to execute direct arithmetic first like [+] [-] [x] [÷] but with [x²] or [√x] can not recall [LSTx] so then I use 1 [x] [X<>Y] first so that I can recall [LSTx] later in program. Gamo RE: (12C) Signum Function - Thomas Klemm - 03-03-2019 08:17 AM (03-01-2019 06:29 AM)Gamo Wrote:   Code: X=0 GTO 00 ENTER ENTER x √x ÷ This doesn't work for $$\left | x \right |\leqslant 10^{-50}$$ or $$\left | x \right | \geqslant 10^{50}$$ due to under- or overflow. Obviously it works for numbers like -113 or 47. But what happens with a number that uses all the 10 digits like 12.78974364? When we square this number we get 163.5775424 which is rounded to 10 digits. The exact value is 163.5775423769204496. Taking the square root of the rounded value 163.5775424, why can we be sure that we get the original number? If we received something off by 1 in the last digit, such as 12.78974363 or 12.78974365, we'd end up with 1.000000001 or 0.999999999 instead. Any thoughts why this can't happen? The reverse isn't true as we know: taking the square root of 3 gives 1.732050808. But the square of this number is rounded to 3.000000001. The exact value is 3.000000001493452864. Kind regards Thomas RE: (12C) Signum Function - Dieter - 03-03-2019 12:38 PM (03-02-2019 08:57 PM)rprosperi Wrote:  But... if the input is zero, you get an error instead of zero output. Yes, sure, in a real program the first steps would be x=0? GTO 000. (03-03-2019 02:31 AM)Gamo Wrote:  Unique HP-12C Platinum in ALG mode I'm not sure if TI Calculator with AOS got the [LSTx] function. Of course not. But that's not my question. The point is that on a TI you don't need all this. There is no need to use LstX or a dummy multiplication. I just want to know whether the proposed key sequence (without 1 x multipliation and without LstX) also works on the 12CP or not. Does it? What happens if you press 123 [÷] [x²] [√x] [=] ? Dieter RE: (12C) Signum Function - Albert Chan - 03-03-2019 01:15 PM (03-03-2019 08:17 AM)Thomas Klemm Wrote:  But what happens with a number that uses all the 10 digits like 12.78974364? When we square this number we get 163.5775424 which is rounded to 10 digits ... Taking the square root of the rounded value 163.5775424, why can we be sure that we get the original number? If a number x had an uncertainty factor 1+ε, (x·(1+ε))² = x²·(1+2ε+ε²) ≈ x²·(1+2ε) So, for squaring, uncertainly doubled (with at most ±½ ulp error). Square root is just a reverse operation, uncertainly cut in half. We would expect squaring, then square root, round-trip back to original number. RE: (12C) Signum Function - Gamo - 03-03-2019 02:39 PM Program this in ALG mode for 12CP [÷] [x²] [√x] [=] 123 [R/S] display 123 123 [CHS] [R/S] display 123 Above routine look like ABS function. Gamo RE: (12C) Signum Function - rprosperi - 03-03-2019 03:34 PM (03-03-2019 02:39 PM)Gamo Wrote:  Program this in ALG mode for 12CP [÷] [x²] [√x] [=] 123 [R/S] display 123 123 [CHS] [R/S] display 123 Above routine look like ABS function. Gamo No, the above program returns -1.00 on a 12CP, as Dieter suspected. SICNR, I had to try my first algebriac mode program on a 12CP... Interesting UPDATE: On the initial 12CP (before the (), Undo and Backspace were added) the above program returns -1.00, but on later models (including the Android emulator) returns 123 as Gamo reported. Clearly a bug, I need to explore past bug reports. Intuitively, the initial behavior is correct and the later is not, though it seems unlikely such a bug has never been noticed and fixed... RE: (12C) Signum Function - Thomas Klemm - 03-03-2019 04:59 PM (03-03-2019 01:15 PM)Albert Chan Wrote:  If a number x had an uncertainty factor 1+ε, (x·(1+ε))² = x²·(1+2ε+ε²) ≈ x²·(1+2ε) There's no "uncertainty" when it comes to rounding numbers. Though this may be related it doesn't explain what's going on. Cheers Thomas RE: (12C) Signum Function - Dieter - 03-03-2019 05:51 PM (03-03-2019 03:34 PM)rprosperi Wrote:  On the initial 12CP (before the (), Undo and Backspace were added) the above program returns -1.00, but on later models (including the Android emulator) returns 123 as Gamo reported. That's really weird. #-) I wonder what happens in similar cases like 2 [+] [√x] [=] or 5 [x] [LN] [x²] [=] I'd expect 3,41 and 12,95 but obviously you never know... Dieter RE: (12C) Signum Function - Gerson W. Barbosa - 03-03-2019 06:52 PM (03-03-2019 05:51 PM)Dieter Wrote:  I wonder what happens in similar cases like 2 [+] [√x] [=] or 5 [x] [LN] [x²] [=] I'd expect 3,41 and 12,95 but obviously you never know... That’s what I get on my TI 57, but on my 12c Prestige (and on the latest Platinums, I presume), arguments on the display cannot be reused after an operator. Thus, in order to get what you want the following sequences should be used: 2 [+] 2 [√x] [=] -> 3.414213562 5 [x] 5 [LN] [x²] [=] -> 12.95145197 IMHO, this is a feature, not a bug. Although the old behavior is apparently more convenient in some situations, I prefer this new one (or at least I would if I chose ALG, which I never will). Gerson. RE: (12C) Signum Function - Gerson W. Barbosa - 03-03-2019 07:03 PM (03-03-2019 08:17 AM)Thomas Klemm Wrote:   (03-01-2019 06:29 AM)Gamo Wrote:   Code: X=0 GTO 00 ENTER ENTER x √x ÷ This doesn't work for $$\left | x \right |\leqslant 10^{-50}$$ or $$\left | x \right | \geqslant 10^{50}$$ due to under- or overflow. The RPN program in post #2 above completely avoids these shortcomings. Gerson. RE: (12C) Signum Function - Albert Chan - 03-03-2019 07:49 PM (03-03-2019 04:59 PM)Thomas Klemm Wrote:  There's no "uncertainty" when it comes to rounding numbers. Though this may be related it doesn't explain what's going on. Turns out, there is nothing to explain. I found a counter-example ... My post on square, following by square-root round-trip is wrong Example, on HP-12C, x = Pi + 0.04 = 3.181592654 3.181592654^2 ≈ 10.1225318160, rounded-up-to 10.12253182 √10.12253182 ≈ 3.18159265463, rounded-up-to 3.181592655 Ratio of x / √(x²) = 0.9999999997 ≠ 1 RE: (12C) Signum Function - rprosperi - 03-03-2019 07:59 PM (03-03-2019 05:51 PM)Dieter Wrote:  I wonder what happens in similar cases like 2 [+] [√x] [=] or 5 [x] [LN] [x²] [=] I'd expect 3,41 and 12,95 but obviously you never know... Your expectations are correct, IMHO. Original 12CP: 3,41 & 12,95 Later 12CP: 1,41 & 2,59 I've not researched yet, but Gerson's summary "...arguments on the display cannot be reused after an operator..." seems a concise summary of the 'fixed' or at least updated, behavior. Denying re-use of the original argument may have been necessary (or at least easier) as a result of adding the UNDO feature in the 2nd and all later series of 12CP models. One could argue either way which is 'right', since algebraic notation is notoriously ambiguous, though I must admit that this is a more interesting than normal situation. @Gerson - of course the RPN solution works; they're always unambiguous. RE: (12C) Signum Function - Thomas Klemm - 03-03-2019 11:27 PM (03-03-2019 07:49 PM)Albert Chan Wrote:  Example, on HP-12C, x = Pi + 0.04 = 3.181592654 If we just look at the mantissa it appears that this only happens for values between $$\sqrt{10} \approx 3.16228$$ and $$5$$. And within this interval it happens at about 18.37% of the cases. Which is close to $$\frac{5 - \sqrt{10}}{10} \approx 0.18377$$. Choosing a number at random leads to a wrong result in about 1 of 27 cases. I don't think that we can neglect that. Here's a table with some values $$x$$ having only 5 digits after the decimal point and $$\sqrt{x^2}$$ as it is calculated by the HP-12C: Code: 3.16343 3.163429999 3.16466 3.164660001 3.17129 3.171289999 3.19029 3.190289999 3.19434 3.194340001 3.20766 3.207660001 3.22016 3.220160001 3.22092 3.220920001 3.22379 3.223789999 3.23338 3.233379999 3.23869 3.238690001 3.24384 3.243840001 3.25258 3.252580001 3.25807 3.258069999 3.26229 3.262289999 3.28479 3.284789999 3.28807 3.288069999 3.29319 3.293190001 3.29693 3.296929999 3.30756 3.307559999 3.32538 3.325379999 3.32808 3.328080001 3.34906 3.349059999 3.35962 3.359619999 3.36371 3.363709999 3.38234 3.382340001 3.38394 3.383939999 3.38838 3.388379999 3.41781 3.417810001 3.42121 3.421209999 3.42931 3.429310001 3.43142 3.431420001 3.44721 3.447209999 3.44906 3.449059999 3.45719 3.457190001 3.46108 3.461080001 3.47871 3.478709999 3.48116 3.481160001 3.48608 3.486080001 3.48643 3.486429999 3.48821 3.488209999 3.49429 3.494289999 3.49831 3.498310001 3.49921 3.499209999 3.50488 3.504879999 3.51834 3.518340001 3.53331 3.533310001 3.54069 3.540690001 3.54171 3.541709999 3.54388 3.543879999 3.54484 3.544840001 3.55221 3.552209999 3.55792 3.557920001 3.56512 3.565119999 3.57588 3.575879999 3.57734 3.577340001 3.57829 3.578289999 3.57866 3.578660001 3.58194 3.581939999 3.59334 3.593340001 3.59421 3.594209999 3.59556 3.595559999 3.60493 3.604929999 3.60834 3.608340001 3.61407 3.614069999 3.61743 3.617429999 3.61884 3.618840001 3.63366 3.633660001 3.63462 3.634619999 3.63771 3.637709999 3.65793 3.657929999 3.66531 3.665310001 3.67357 3.673569999 3.67557 3.675569999 3.67762 3.677619999 3.68834 3.688340001 3.69693 3.696929999 3.70616 3.706160001 3.71143 3.711429999 3.71466 3.714660001 3.72621 3.726209999 3.74243 3.742429999 3.75288 3.752879999 3.75638 3.756379999 3.75643 3.756429999 3.75938 3.759379999 3.77419 3.774190001 3.78007 3.780069999 3.79638 3.796379999 3.79731 3.797310001 3.80288 3.802879999 3.82866 3.828660001 3.84429 3.844289999 3.85679 3.856789999 3.87116 3.871160001 3.88507 3.885069999 3.89021 3.890209999 3.90788 3.907879999 3.91862 3.918619999 3.94979 3.949789999 3.95034 3.950340001 3.96016 3.960160001 3.96812 3.968119999 3.99571 3.995709999 4.00312 4.003119999 4.01279 4.012789999 4.01316 4.013160001 4.02188 4.021879999 4.07884 4.078840001 4.11312 4.113119999 4.15243 4.152429999 4.17866 4.178660001 4.19862 4.198619999 4.23857 4.238569999 4.24734 4.247340001 4.26734 4.267340001 4.28816 4.288160001 4.29112 4.291119999 4.30134 4.301340001 4.30566 4.305660001 4.33416 4.334160001 4.36738 4.367379999 4.38716 4.387160001 4.40307 4.403069999 4.44193 4.441929999 4.51507 4.515069999 4.51557 4.515569999 4.51793 4.517929999 4.57107 4.571069999 4.58657 4.586569999 4.64793 4.647929999 4.68393 4.683929999 4.72493 4.724929999 4.73393 4.733929999 4.77593 4.775929999 4.84307 4.843069999 Cheers Thomas RE: (12C) Signum Function - Albert Chan - 03-04-2019 02:02 AM (03-03-2019 11:27 PM)Thomas Klemm Wrote:  If we just look at the mantissa it appears that this only happens for values between $$\sqrt{10} \approx 3.16228$$ and $$5$$. This range can be explained by squaring errors: (x·(1+ε))² = x²·(1+2ε+ε²) ≈ x²·(1+2ε) Assuming numbers in scientific notation, with mantissa = [1, 10), ulp = 1e-9 Max rel error of √(x²) ≈ ½ Max rel error of x² = ½ (½ ulp / mantissa(x²)) Max abs error of √(x²) ≈ ¼ ulp (mantissa(x) / mantissa(x²)) Round-trip errors occurs if √(x²) abs error ≥ ½ ulp -> mantissa(x) ﻿ ﻿ ﻿ ﻿between √10 and 5.0 -> Max abs error between 0.79 and 0.50 ulp Edit: this also explained why (√x)² may have errors upto 3 ULP Example: (√9.11)² = 9.110000003 RE: (12C) Signum Function - Gamo - 03-04-2019 04:58 AM Example program on ALG mode Guass Sum (x² + x) ÷ 2 ------------------------------------- If program this way [LSTx] [x²] + [LSTx] [÷] 2 [=] Each [R/S] need to clear register to restart Clear by pressing [CLx] [CLx] 100 [R/S] display 5050 If not clear 100 [R/S] display 12751300 --------------------------------------------- This routine no need to clear register but Need the dummy (1 × X<>Y) 1 [×] [X<>Y] [x²] + [LSTx] [÷] 2 [=] --------------------------------------------- Gamo