Nice thanks for that.

The question remains why prefer hypot over hypot2?

On Saturday, March 26, 2016 at 5:28:45 PM UTC-4, Jeffrey Sarnoff wrote:
>
> there is that and more substantively imo, there  is this (so, *nevermind 
>> .. there is conformance)*:
>
> For example, 0*NaN must be NaN because 0*¥ is an INVALID operation ( NaN 
>> ). On the other hand, for hypot(x, y) := Ö(x*x + y*y) we find that hypot(¥, 
>> y) = +¥ for all real y , finite or not, and deduce that hypot(¥, NaN) = +¥ 
>> too; naive implementations of hypot may do differently.    
>
>     -- Lecture Notes on the Status of IEEE Standard 754 for Binary 
>> Floating-Point Arithmetic   IEEE 754 Status: William Kahan, 1997 
>> <https://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF>  (page 
>> 7)  
>
>
> For the hypot function, hypot(±0, ±0) is +0, hypot(±∞, qNaN) is +∞, and 
>> hypot(qNaN, ±∞) is +∞  -- IEEE 754-2008 (page 43) 
>
>  
>
>  
>
>>  
>
> For the hypot function, hypot(±0, ±0) is +0, hypot(±∞, qNaN) is +∞, and 
>> hypot(qNaN, ±∞) is +∞
>
>
> On Saturday, March 26, 2016 at 4:11:43 PM UTC-4, feza wrote:
>>
>> Actually I don't know if this is intended behavior or not for example  in 
>> MATLAB hypot(NaN,Inf) and hypot(Inf,NaN) both give NaN
>>
>> BUT
>> http://en.cppreference.com/w/c/numeric/math/hypot
>> specifies that even if one of the arugments is NaN hypot returns +Inf
>>
>>
>> On Saturday, March 26, 2016 at 3:54:22 PM UTC-4, feza wrote:
>>>
>>> Good catch Jeffrey. I will file a bug report!
>>>
>>> On Saturday, March 26, 2016 at 3:50:31 PM UTC-4, Jeffrey Sarnoff wrote:
>>>>
>>>> Looking at your note, I noticed this:
>>>>
>>>> *     hypot(Inf,NaN) == hypot(NaN,Inf) == Inf*
>>>>
>>>> That cannot be correct because *sqrt(x^2 + NaN^2) => sqrt(x^2 + NaN) 
>>>> => sqrt(NaN) => NaN*
>>>>
>>>> On Saturday, March 26, 2016 at 3:23:32 PM UTC-4, feza wrote:
>>>>>
>>>>> Why is hypot1 preferred (in Base) over hypot2 ? To me it seems better 
>>>>> to just return y    in the one commented line
>>>>>
>>>>> function hypot2{T<:AbstractFloat}(x::T, y::T)                         
>>>>>        
>>>>>     x = abs(x)                                                         
>>>>>       
>>>>>     y = abs(y)                                                         
>>>>>       
>>>>>     if x < y                                                           
>>>>>       
>>>>>         x, y = y, x                                                   
>>>>>        
>>>>>     end                                                               
>>>>>        
>>>>>     if x == 0                                                         
>>>>>        
>>>>>         return y        ## compare with below                         
>>>>>                             
>>>>>     else                                                               
>>>>>       
>>>>>         r = y/x                                                       
>>>>>        
>>>>>         if isnan(r)                                                   
>>>>>        
>>>>>             isinf(x) && return x                                       
>>>>>       
>>>>>             isinf(y) && return y                                       
>>>>>       
>>>>>             return r                                                   
>>>>>       
>>>>>         end                                                           
>>>>>        
>>>>>     end                                                               
>>>>>        
>>>>>     x * sqrt(one(r)+r*r)                                               
>>>>>       
>>>>> end                                                                   
>>>>>        
>>>>>
>>>>>
>>>>>  function hypot1{T<:AbstractFloat}(x::T, y::T)                         
>>>>>                         
>>>>>      x = abs(x)                                                       
>>>>>                          
>>>>>      y = abs(y)                                                       
>>>>>                          
>>>>>      if x < y                                                         
>>>>>                          
>>>>>          x, y = y, x                                                   
>>>>>                         
>>>>>      end                                                               
>>>>>                         
>>>>>      if x == 0                                                         
>>>>>                         
>>>>>          r = y/one(x)   # Why not just return y?                       
>>>>>    
>>>>>      else                                                             
>>>>>                          
>>>>>          r = y/x                                                       
>>>>>                         
>>>>>          if isnan(r)                                                   
>>>>>                         
>>>>>              isinf(x) && return x                                     
>>>>>                          
>>>>>              isinf(y) && return y                                     
>>>>>                          
>>>>>              return r                                                 
>>>>>                          
>>>>>          end                                                           
>>>>>                         
>>>>>      end                                                               
>>>>>                         
>>>>>      x * sqrt(one(r)+r*r)                                             
>>>>>                          
>>>>>  end                                                                   
>>>>>                         
>>>>>
>>>>

Reply via email to