Re: Dead Reckoning

2015-10-13 Thread Clark S. Cox III

> On 2015/10/09, at 22:24, Michael de Haan   wrote:
> 
> let secondsToHours = 2.778E-4

Only tangentially related, but that value (i.e. 1 / 3600) has a hex 
representation that is almost too beautiful not to use:
0x1.23456789ABCDEFp-12

:)
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Rick Mann

> On Oct 9, 2015, at 21:13 , Graham Cox  wrote:
> 
> 
>> On 10 Oct 2015, at 1:58 am, Michael de Haan   wrote:
>> 
>> func degrees2radians(coordinate:CLLocationDegrees) -> Double {
>> 
>>  return (M_PI * coordinate ) / 180.00
>> }
>> 
>> func radians2degrees(coordinate:CLLocationDegrees) -> Double {
>> 
>>  return (180.00 * coordinate ) / M_PI
>> }
>> 
> 
> JFYI:
> 
> It might be worth noting that for performance crtical code, these two 
> functions can be replaced by simple multiply-by-a-constant macros:
> 
> 
> #define   DEGREES_TO_RADIANS( d ) ((d) * 
> 0.0174532925199432958)
> #define   RADIANS_TO_DEGREES( r ) ((r) * 
> 57.29577951308232)

The compiler should figure that out either way it's written.

-- 
Rick Mann
rm...@latencyzero.com



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Marco S Hyman
The code is in Swift. #define is not an option.

> There’s very little reason to use macros for things like this, when an inline 
> function is as efficient and safer.

IMHO function calls also help readability. My version looks like this:

private let π = M_PI
private let d2r = π / 180   // degrees to radians adjustment
private let r2d = 180 / π   // radians to degrees adjustments

private func degreesToRadians(degrees: Double) -> Double {
return degrees * d2r
}

private func radiansToDegrees(radians: Double) -> Double {
return radians * r2d
}


Yeah, "π = M_PI" wasn’t really needed.

Marc

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Rick Mann

> On Oct 9, 2015, at 21:24 , Graham Cox  wrote:
> 
> 
>> On 10 Oct 2015, at 3:21 pm, Rick Mann  wrote:
>> 
>> The compiler should figure that out either way it's written.
> 
> 
> Does it actually replace a function call with an inline constant multiply?

And it wouldn't be function calls, but instructions, something like

MUL
DIV

I could be wrong about it doing it without optimizations turned on, the more I 
think about it.


-- 
Rick Mann
rm...@latencyzero.com



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Graham Cox

> On 10 Oct 2015, at 3:21 pm, Rick Mann  wrote:
> 
> The compiler should figure that out either way it's written.


Does it actually replace a function call with an inline constant multiply?

—Graham



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Michael de Haan 
Thank you for all your input. 



I made one small little change to accept time input in seconds.

>>


let secondsToHours = 2.778E-4

..

let rDistance = ((speed * time * knotsToMeters * secondsToHours) / earthRadius)

<<


> On Oct 9, 2015, at 9:33 PM, Rick Mann  wrote:
> 
> 
>> On Oct 9, 2015, at 21:24 , Graham Cox  wrote:
>> 
>> 
>>> On 10 Oct 2015, at 3:21 pm, Rick Mann  wrote:
>>> 
>>> The compiler should figure that out either way it's written.
>> 
>> 
>> Does it actually replace a function call with an inline constant multiply?
> 
> And it wouldn't be function calls, but instructions, something like
> 
> MUL
> DIV
> 
> 
> 
> 






___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Graham Cox

> On 10 Oct 2015, at 1:58 am, Michael de Haan   wrote:
> 
> func degrees2radians(coordinate:CLLocationDegrees) -> Double {
> 
>   return (M_PI * coordinate ) / 180.00
> }
> 
> func radians2degrees(coordinate:CLLocationDegrees) -> Double {
> 
>   return (180.00 * coordinate ) / M_PI
> }
> 

JFYI:

It might be worth noting that for performance crtical code, these two functions 
can be replaced by simple multiply-by-a-constant macros:


#define DEGREES_TO_RADIANS( d ) ((d) * 0.0174532925199432958)
#define RADIANS_TO_DEGREES( r ) ((r) * 
57.29577951308232)


—Graham
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Rick Mann

> On Oct 9, 2015, at 21:24 , Graham Cox  wrote:
> 
> 
>> On 10 Oct 2015, at 3:21 pm, Rick Mann  wrote:
>> 
>> The compiler should figure that out either way it's written.
> 
> 
> Does it actually replace a function call with an inline constant multiply?

Yeah, it should. Probably even with optimizations turned off.



-- 
Rick Mann
rm...@latencyzero.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Jens Alfke

> On Oct 9, 2015, at 9:24 PM, Graham Cox  wrote:
> 
> Does it actually replace a function call with an inline constant multiply?

Sure. With optimizations enabled, the compiler is likely to inline the function 
calls, which will turn them into a simple multiplication. (This applies to any 
of C, Obj-C, C++, or Swift.) In C-based languages you can use the “inline” 
keyword to further encourage the compiler.

There’s very little reason to use macros for things like this, when an inline 
function is as efficient and safer.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Dead Reckoning

2015-10-09 Thread Graham Cox

> On 10 Oct 2015, at 3:54 pm, Jens Alfke  wrote:
> 
> There’s very little reason to use macros for things like this, when an inline 
> function is as efficient and safer.
> 


Probably another example of modern technology passing me by - used these macros 
for >20 years…  ;)

Here’s a quick test I ran, disassembling a simple C file without and then with 
optimisations. The non-opt case makes the function call, the opt case goes even 
further and for my test case just loads a constant, since in my test case it 
never changes. That suggests it really does optimise away the function call 
(though at this stage it’s still present - presumably the lnker’s dead-code 
stripper would remove the actual function).

No optimisation:


LCPI0_0:
.quad   4614256656552969552 ## double 3.141592654001
LCPI0_1:
.quad   4640537203540230144 ## double 180
.section__TEXT,__text,regular,pure_instructions
.globl  _radians_to_degrees
.align  4, 0x90
_radians_to_degrees:## @radians_to_degrees
Lfunc_begin0:
.file   1 "/Users/grahamcox/Projects/Test" "testoptimaisation.c"
.loc1 13 0  ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:13:0
.cfi_startproc
## BB#0:
pushq   %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq%rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
movsd   LCPI0_0(%rip), %xmm1
movsd   LCPI0_1(%rip), %xmm2
movsd   %xmm0, -8(%rbp)
.loc1 15 2 prologue_end ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:15:2
Ltmp3:
mulsd   -8(%rbp), %xmm2
divsd   %xmm1, %xmm2
movaps  %xmm2, %xmm0
popq%rbp
retq
Ltmp4:
Lfunc_end0:
.cfi_endproc

.section__TEXT,__literal8,8byte_literals
.align  3
LCPI1_0:
.quad   4615288898129284301 ## double 3.6001
.section__TEXT,__text,regular,pure_instructions
.globl  _test
.align  4, 0x90
_test:  ## @test
Lfunc_begin1:
.loc1 21 0  ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:21:0
.cfi_startproc
## BB#0:
pushq   %rbp
Ltmp5:
.cfi_def_cfa_offset 16
Ltmp6:
.cfi_offset %rbp, -16
movq%rsp, %rbp
Ltmp7:
.cfi_def_cfa_register %rbp
subq$32, %rsp
movsd   LCPI1_0(%rip), %xmm0
.loc1 22 2 prologue_end ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:22:2
Ltmp8:
movsd   %xmm0, -8(%rbp)
.loc1 24 13 ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:24:13
movsd   -8(%rbp), %xmm0
callq   _radians_to_degrees
leaqL_.str(%rip), %rdi
movsd   %xmm0, -16(%rbp)
.loc1 26 2  ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:26:2
movsd   -16(%rbp), %xmm0
movb$1, %al
callq   _printf
.loc1 27 1  ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:27:1
movl%eax, -20(%rbp) ## 4-byte Spill
addq$32, %rsp
popq%rbp
retq
Ltmp9:
Lfunc_end1:
.cfi_endproc

With full optimisation, :


LCPI0_0:
.quad   4640537203540230144 ## double 180
LCPI0_1:
.quad   4614256656552969552 ## double 3.141592654001
.section__TEXT,__text,regular,pure_instructions
.private_extern _radians_to_degrees
.globl  _radians_to_degrees
_radians_to_degrees:## @radians_to_degrees
Lfunc_begin0:
.file   1 "/Users/grahamcox/Projects/Test" "testoptimaisation.c"
.loc1 13 0  ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:13:0
.cfi_startproc
## BB#0:
pushq   %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq%rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
##DEBUG_VALUE: radians_to_degrees:rad <- XMM0
.loc1 15 2 prologue_end ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:15:2
Ltmp3:
mulsd   LCPI0_0(%rip), %xmm0
divsd   LCPI0_1(%rip), %xmm0
popq%rbp
retq
Ltmp4:
Lfunc_end0:
.cfi_endproc
.section__TEXT,__literal8,8byte_literals
.align  3
LCPI1_0:
.quad   4641461314255121455 ## double 206.26480622016376
.section__TEXT,__text,regular,pure_instructions
.private_extern _test
.globl  _test
_test:  ## @test
Lfunc_begin1:
.loc1 21 0  ## 
/Users/grahamcox/Projects/Test/testoptimaisation.c:21:0
.cfi_startproc
## BB#0:
pushq   %rbp
Ltmp5:
.cfi_def_cfa_offset 16
Ltmp6:
.cfi_offset %rbp, -16
movq%rsp, 

Re: Dead Reckoning

2015-10-09 Thread Michael de Haan 

> 
> I am trying to derive a DR fix from an initialized CLLocation.
> 
> Like this. (Playground)
> 
> let fixTime = NSDate(timeInterval: (1.00 * 60.00 * 60.00  * -1.00), 
> sinceDate: NSDate()) // one hour ago
> let fixLocation = CLLocation(coordinate: 
> CLLocationCoordinate2DMake(boatLocation.coordinate.latitude, 
> boatLocation.coordinate.longitude), altitude: 0.00, horizontalAccuracy: 0.00, 
> verticalAccuracy: 0.00,course: heading, speed: knots * 0.5145,  
> timestamp: fixTime)  // location one hour ago
> 
> 
> I am sure there must be something like…..
> 
> 

For those ever needing this, here is what I came up with. (Heavy credit to SO 
for drLat, drLong and conversation  formulae)


let knotsToMeters =  1852.00
let earthRadius = 6372797.6

func degrees2radians(coordinate:CLLocationDegrees) -> Double {

return (M_PI * coordinate ) / 180.00
}

func radians2degrees(coordinate:CLLocationDegrees) -> Double {

return (180.00 * coordinate ) / M_PI
}


func deadReckonedLocationFrom(fix: CLLocationCoordinate2D, 
bearing:CLLocationDirection, knots:CLLocationSpeed, 
timeSinceReport:NSTimeInterval) -> CLLocationCoordinate2D {


let rBearing = degrees2radians(bearing)
let rFixLat = degrees2radians(fix.latitude)
let rFixLong = degrees2radians(fix.longitude)
let rDistance = (knots * timeSinceReport * knotsToMeters) / earthRadius

let drLat =  asin(sin(rFixLat) * cos(rDistance) + cos(rFixLat) * 
sin(rDistance) * cos(rBearing))
let drLong = rFixLong + atan2(sin(rBearing) * sin(rDistance) * 
cos(rFixLat), cos(rDistance) - sin(rFixLat) * sin(drLat))
return CLLocationCoordinate2D(latitude: radians2degrees(drLat), longitude: 
radians2degrees(drLong))
}


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Dead Reckoning

2015-10-08 Thread Michael de Haan 
I am trying to derive a DR fix from an initialized CLLocation.

Like this. (Playground)

let fixTime = NSDate(timeInterval: (1.00 * 60.00 * 60.00  * -1.00), sinceDate: 
NSDate()) // one hour ago
let fixLocation = CLLocation(coordinate: 
CLLocationCoordinate2DMake(boatLocation.coordinate.latitude, 
boatLocation.coordinate.longitude), altitude: 0.00, horizontalAccuracy: 0.00, 
verticalAccuracy: 0.00,course: heading, speed: knots * 0.5145,  timestamp: 
fixTime)  // location one hour ago


I am sure there must be something like…..


let drLocation = fixLocation.advanceBySomeArbitraryTime(arbitraryTime)
But, this has eluded me so far. Any help would be welcome.
Thank you.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com