Skip to content
Tcl Source Code History Of Ticket 3530004
Login

History Of Ticket 3530004

Artifacts Associated With Ticket 3530004

  1. Ticket change [6dc907c647] (rid 128761) by zoltan3 on 2012-05-26 20:14:57:

    1. assignee initialized to: "dkf"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized to:
      There are functions double, int, floor and ceil for rounding floating point values in various mode, but e.g. floor($num) and ceil($num)  are equivalent with round($num -+ 0.5). But the signum function can be implemented only with longer constructs.
      
    4. foundin initialized to: "None"
    5. is_private initialized to: "0"
    6. priority initialized to: "5 Medium"
    7. resolution initialized to: "None"
    8. status initialized to: "Open"
    9. submitter initialized to: "zoltan3"
    10. subsystem initialized to: "16. Commands A-H"
    11. title initialized to: "New funcion in "expr": sgn()"
    12. type initialized to: "RFE"
  2. Ticket change [98f4c06368] (rid 110698) by zoltan3 on 2012-05-28 17:12:40:

    1. icomment:
      I would take back my example: "floor($num) and ceil($num) are equivalent with round($num -+ 0.5)" it is not right.
      But not this is the point now.
      
      
    2. login: "zoltan3"
    3. mimetype: "text/plain"
    4. username: "zoltan3"
  3. Ticket change [1df76aeb53] (rid 90897) by dkf on 2012-05-28 17:31:12:

    1. icomment:
      Here's a complete workaround (modulo ensuring that $x is numeric and handling NaN):
      
        proc tcl::mathfunc::sgn {x} {
            expr {$x < 0 ? -1 : $x > 0 ? 1 : 0}
        }
      
      The only question is whether Tcl should provide such a thing by default.
      
      
    2. login: "dkf"
    3. mimetype: "text/plain"
    4. title changed to: "New function in "expr": sgn()"
    5. username: "dkf"
  4. Ticket change [7494ae4024] (rid 105223) by twylite on 2012-05-28 18:08:46:

    1. icomment:
      is sgn() meant to be "sign()" as in DKF's example, or is it meant to be "round to a number of significant digits" as discussed below?
      
      In Excel for example, =FLOOR(1.134, 0.01) rounds 1.134 down to the nearest multiple of 0.01, giving 1.13.  Similarly =FLOOR(1.5, 0.4) gives 1.2.  The =ROUND() function rounds to a number of digits, e.g. =ROUND(1.135, 2) gives 1.14.
      
      The Excel functions combine a mathematical operation and an output formatting operation.  The result of =FLOOR(1.134, 0.01) may not be accurately represented by a float or a double, so the floating point value is truncated for display purposes.
      
      Rounding to a significant number of digits (as in =ROUND) as opposed to an exact significance (as in =FLOOR) is quite easy in Tcl:
      
      proc round {d sgn} { format %0.${sgn}f $d }
      
      Computing and displaying floor or ceiling to a specific precision is more complicated:
      
      proc floor {d sgn} { 
        set prec [expr { int(ceil(-log($sgn)/log(10))) }]
        format %0.${prec}f [expr { floor($d / $sgn) * $sgn }] 
      }
      
      A compromise may be to extend round, floor and ceil to take an optional second parameter (significance), and implement equivalent to:
      
      proc round {d {sgn 1}} { expr { round($d / $sgn) * $sgn } }
      proc floor {d {sgn 1}} { expr { floor($d / $sgn) * $sgn } }
      
      This gives a mathematically accurate answer (within the constraints of floating point arithmetic), but does not attempt to format/truncate the output.  This would be appropriate behaviour for [expr] in my opinion (but including formatting behaviour would not be appropriate).
      
      
      
      
      
      
      
    2. login: "twylite"
    3. mimetype: "text/plain"
    4. username: "twylite"