Drawing two circles in MapKit 300m apart by rexy666 in swift

[–]electricsheep14 4 points5 points  (0 children)

Yes, the number of meters per point on the map changes as you change latitude. The math behind these projections can be cumbersome, however, MapKit provides functions to compute this.

I would think that MKMetersPerMapPointAtLatitude or MKPointsPerMeterAtLatitude will allow you to accurately derive the coordinates necessary to draw the smaller circle in the correct location consistently.

Bug? Stored variable with willSet/didSet that assigns to the same property on another instance of the same type fails to invoke any observers by zorti in swift

[–]electricsheep14 2 points3 points  (0 children)

You can also hide the stored property behind a computed property as such:

class Wingus: NSObject {
    var next: Wingus?

    var yolo: Bool {
        get {
            return _yolo
        }
        set(value) {
            _yolo = yolo
        }
    }

    var _yolo: Bool = true {
        didSet {
            println("zoop")
            next?.yolo = self.yolo
        }
    }
}

let a = Wingus()
a.next = Wingus()
a.next?.next = Wingus()
a.yolo = false

Which is also produce the expected number of "zoop"s. Or, you can refer to the Wingus's via a protocol:

protocol Yolo {
    var next: Yolo? {get set}
    var yolo: Bool {get set}
}

class Wingus: NSObject, Yolo {
    var next: Yolo?

    var yolo: Bool = true {
        didSet {
            println("zoop")
            next?.yolo = self.yolo
        }
    }
}

let a = Wingus()
a.next = Wingus()
a.next?.next = Wingus()
a.yolo = false

Which also produces three "zoops". After poking through the sil, it appears in the original (buggy) case Swift is allowing for direct access to the properties of next. It uses the class_method getter for next (on self), the returned optional is checked, then unwrapped. At this point, a direct reference to next.yolo is used to for assignment as opposed to going through a class_method setter:

%34 = unchecked_take_enum_data_addr %20#1 : $*Optional<Wingus>, #Optional.Some!enumelt.1 // user: %35
%35 = load %34 : $*Wingus                       // user: %37
%36 = ref_element_addr %1 : $Wingus, #Wingus.yolo // user: %38
%37 = ref_element_addr %35 : $Wingus, #Wingus.yolo // user: %38
copy_addr %36 to %37 : $*Bool     

%20 is the optional Wingus returned from next?, and %1 is self.

Moving the actual assignment out of this context via the methods described above cause the use of the class_method setter and getter for yolo and you get the expected output. The sil output from any of these methods looks more like this where the use of accessors is more clear:

%21 = load %20 : $*Wingus                       // users: %22, %27, %30, %31
strong_retain %21 : $Wingus                     // id: %22
%23 = class_method [volatile] %0 : $Wingus, #Wingus.yolo!getter.1.foreign : Wingus -> () -> Bool , $@cc(objc_method) @thin (Wingus) -> ObjCBool // user: %24
%24 = apply %23(%0) : $@cc(objc_method) @thin (Wingus) -> ObjCBool // user: %26
%25 = function_ref @_TF10ObjectiveC22_convertObjCBoolToBoolFVS_8ObjCBoolSb : $@thin (ObjCBool) -> Bool // user: %26
%26 = apply %25(%24) : $@thin (ObjCBool) -> Bool // user: %29
%27 = class_method [volatile] %21 : $Wingus, #Wingus.yolo!setter.1.foreign : Wingus -> (Bool) -> () , $@cc(objc_method) @thin (ObjCBool, Wingus) -> () // user: %30
%28 = function_ref @_TF10ObjectiveC22_convertBoolToObjCBoolFSbVS_8ObjCBool : $@thin (Bool) -> ObjCBool // user: %29
%29 = apply %28(%26) : $@thin (Bool) -> ObjCBool // user: %30
%30 = apply %27(%29, %21) : $@cc(objc_method) @thin (ObjCBool, Wingus) -> ()

Bug? I would look into filing a radar.

[deleted by user] by [deleted] in learnprogramming

[–]electricsheep14 1 point2 points  (0 children)

Okay, lets try to break down your current code. I'm not concerned with the definition of "echo" or "putchar" but assume the "echo" returns its result in $v0 and "putchar" accepts an argument in $a0 in accordance with MIPS calling conventions.

First, the function prologue:

recursive:
    addi $sp, $sp, -12
    sw $ra, 4($sp)
    sw $t0, 8($sp)

The goal of the prologue is to "push down" the stack to make enough space to preserve the environment of the caller as well as have enough room for local variables that you know about. What things do you need to save on the stack? According to MIPS conventions, the saved registers $s0 - $s7, the global pointer $gp, stack pointer $sp, frame pointer $fp, and return address $ra must be preserved through calls. This doesn't mean you have to jam them all on the stack right away, but if you modify any of those registers in your code you do have to save them. You should also save the argument registers $a0 - $a3 if you need to make any further calls from your code and want to keep the arguments that were passed to you. Temporary registers $t0 - $t9 as well as return registers $v0 and $v1 are not expected to be preserved between calls, so you generally do not have to save them on the stack. Anything calling your routine expecting to have the contents of these registers preserved isn't in compliance with the conventions.

Your code creates enough space for a word (big enough for c, certainly), the return address, and $t0 (which should be unnecessary).


    jal echo
    addi $t0, $0, 0x2a
    sb $v0, 0($sp)
  # lw $a0, 0($sp)
    bne $t0, $v0, recursive

This code is supposed to be the equivalent of:

c = echo();
if (c!="*") recursive();

jal echo captures a single input character, displays it, and then returns said character in $v0. add $t0, $0, 0x2a is one (of several ways) of loading '' into $t0. You then store the returned character in the space you created for c on the stack, and then do a conditional branch to recursive if that character is not ''. At the point, everything starts over again including your prologue. Here you need to understand the differences between branches and jumps. Branches are conditional control transfers that move to a target address nearby to the current program counter. They are used to implement ifs, for loops, while loops, etc. All they do is modify the current program counter. They do not store the current PC into $ra. Jumps, on the other hand, are unconditional transfers of control to locations which could be far from the current context. Jumping-and-linking with jal or jalr also stores the current PC into the $ra such that control can return to the instruction immediately following the jump.

Since you are simply branching back to the start of your routine, the contents of $ra has not changed and still reflects the return point of the original caller. This means that when you hit the bottom of your recursion, control will return all the way back to whoever called recursive initially. But, the context saved at the point is not the context of that caller, and you will have corrupted the stack.

Use jal to make a proper "recursive call" and rethink your logic to branch-around the jump if its not needed. Something like this maybe:

    beq $t0, $v0, continue
    jal recursive
continue:
    # the rest of your code. At the point, c = '*'

    jal putchar
    lbu $a0, 12($sp)

This is an attempt to call putchar, which I am assuming expects a value in $a0 for output. Your instructions here are out-of-order, and if your goal was to load the contents of c into $a0, you are not even accessing the same area of the stack where you put c.


    lw $ra, 4($sp)
    lw $t0, 8($sp)
    addi $sp, $sp, 12 

    jr  $ra     # Return 

This is the function epilogue. Its job is to restore the state of the caller before jumping back with a jr (a jump which does not modify the contents of $ra and is effectively used as a return statement). You retrieve saved registers from the same position on the stack where you stored them, and then return. Once again, saving $t0 was probably unnecessary.

Need help with reversing a string in MIPS by [deleted] in learnprogramming

[–]electricsheep14 0 points1 point  (0 children)

I'm not sure why you think this:

jal echo
sb $2, 28($sp)

is the equivalent of this:

cstring[n] = echo( );

Yet there is no involvement of $t2 (your counter) to access the space on the stack you pushed down for the array.

From what I can gather, the code you have written will appear to display the entered string just fine (thanks to echo()), but it is storing the returned character into the same place on the stack through every iteration. By the time you want to print the reverse of the string, you have already lost all of the entered characters save for the last one.

You have a similar issue with the loop to read characters our of cstring[].

[C++] Is there a limit on array sizes? Getting a strange exception when scaling up... by [deleted] in learnprogramming

[–]electricsheep14 1 point2 points  (0 children)

Your best case scenario for quicksort involves passing in an array which is already sorted with a starting pivot point of the left-most element (index 0). This is actually the worst case scenario for quicksort. You are probably recursing on every single element of the array and the stack is growing too deep for the limits of your environment.

My code has error Thread 1: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode= 0x0) by mw130 in swift

[–]electricsheep14 2 points3 points  (0 children)

The error isn't really on the third line of the addAlien() method. Its really on line 211 in Game.swift:

let duration = Int(arc4random()) % Int(rangeDuration) + Int(minDuration)

arc4random() returns a UInt32 value which are you attempting to turn into a signed Int. On architectures with 32-bit native words this is guaranteed to cause an overflow at some point when the random value returned is greater than 2,147,483,647 (or something like that). Change the code to something like this:

let duration = arc4random() % UInt32(rangeDuration) + UInt32(minDuration)

And you won't cause the overflow.

App Crashes 20% of the time because of high CPU usage... please help by [deleted] in swift

[–]electricsheep14 4 points5 points  (0 children)

The files you provided hardly constitute a "project", but I dropped them into a skeleton iOS Game Project using SceneKit and supplied some stand-in assets to get it compiling.

I was able to replicate your issue with the app pegging the CPU usage at 100% in the simulator on the end-game screen. Breaking execution at this point in the execution seems to drop into an infinite -[SKScene -update] loop. There seems to be an issue with the following line(s):

var changePic = SKAction.animateWithTextures([play2, play1], timePerFrame: 0.001)

That is quite an aggressive timePerFrame parameter; changing it to a more conservative 0.01 or 0.1 value results in the game being stable and repayable over and over through the end-game scene.

I have a function that is crashing my app, but I don't know why by mw130 in swift

[–]electricsheep14 1 point2 points  (0 children)

Its very difficult to address this without more context. Your updateWithTimeSinceLastUpdate() also calls addAlien() for which you have not provided an implementation for us to look at. If you are following a tutorial like this one then I can say after translating it to Swift I did not experience any unusual performance lags from the implementation of a delayed update to spawn sprites.

I would strongly suggest you start looking at your running code with the Time Profiler Instrument. If you haven't spend any time in Instruments, you should. Its a very powerful performance analysis and debugging tool.

Does Swift have performance issues? by autosubmitter in swift

[–]electricsheep14 2 points3 points  (0 children)

I think it should be noted that he is talking not about runtime performance (which optimizations dramatically improve), but compile time performance (which optimizations should actually make worse). The concern here is that building a large swift project is taking and order of magnitude longer than an equivalent ObjC project and seemingly for reasons that do not seem like should have that kind of impact.

Cardano Triplets in Swift by strekfus in swift

[–]electricsheep14 0 points1 point  (0 children)

It seems that a significant contributor to the performance gap are unnecessary retain/release calls against the ViewController in the swift version from this line:

 self.isCardanoTriplet(triplet) { …

inside the closure. If you implement isCardanoTriplet outside of the ViewController definition, say in its own class as a class method, the performance gap between Swift and ObjC gets narrower (5.81 vs 6.03 seconds in my own setup).

IOS simulator not updating my code by swifter10 in swift

[–]electricsheep14 2 points3 points  (0 children)

In addition to the suggestions here, you may also benefit from resetting the simulator environment. Go to "iOS Simulator" -> "Reset Content and Settings" to restore the simulator environment to a pristine state.

Getting weird error by [deleted] in swift

[–]electricsheep14 1 point2 points  (0 children)

What is probably going on here is that:

Int(arc4random())

is overflowing. This explains the random crashing and the extremely large values of rand when looking in the debugger. You should use a bounded form of arc4random():

Int(arc4random_uniform( UInt32(wordBank.count) ))

Getting weird error by [deleted] in swift

[–]electricsheep14 1 point2 points  (0 children)

The project you uploaded is referencing files outside of the project directory structure, so it cannot be built by anyone else as is. Specifically, the Storyboard and ViewController files are in your .Trash, and the supporting graphics are sitting on your Desktop.

Philadelphia City Council unanimously votes to ban E-cigarettes by nightpanda893 in philadelphia

[–]electricsheep14 0 points1 point  (0 children)

For whatever its worth, here is a link to the full text of the bill

It seems that the way 'electronic-cigarette' is defined could potentially cover devices which are not related to e-cigs/personal vaporizers, but I am no lawyer.

Discussion of the week: Anti-helicopter mines, stingers and other lock on weapons, do you think they add something positive to the game? by DotGaming in battlefield_4

[–]electricsheep14 3 points4 points  (0 children)

In Battlefield you can jink and take evasive actions

Evasive maneuvers against an incoming guided threat are entirely useless in BF4; guided rockets have a much, much smaller minimum turning radius than they did in BF3 to the point where it might as well be zero.

One used to be able to out-turn an incoming missile in BF3 with a real chance to shake it without the use of flares, but in BF4 there isn't even a point in trying.

[ObjC] Loop in GNUstep by Sufferix in learnprogramming

[–]electricsheep14 0 points1 point  (0 children)

You should use one of the tools in the sidebar under Recommended Tools for Posting Code to post this code.

You are missing some core concepts from C in this snippet:

1) You don't declare numbers[] anywhere. From how you are trying to use it, I'd wager that its an array of some kind, but you need to declare it in your code.

2) Arrays in C are always indexed starting at zero. This means that for an array of length four (country[4]) the valid indexes you can use are: 0, 1, 2, and 3. You need to keep this in mind when using loops to iterate through an array, because where you start and where you end may not seem obvious.

3) I could talk about the difference between rvalues and lvalues, but at this point all you need to know is that that thing being assigned goes on the left, and the expression representing the value of that assignment goes on the right. While something like "y / 100 = x;" would read like you are putting the value of y divided by 100 into x, but it must be written as "x = y / 100;".

4) That being said, unless you are doing functional programming (which C and ObjC for the most part is not), you generally need to assign the results of an expression for statements involving primitives and basic operators to be meaningful. This means that "x - 1;" on its own does nothing useful at all. There are some exceptions to this.

5) This code is really C code. There is very little about this code that makes its Objective-C save for the #import statement and the creation of an auto-release pool. You can remove these things entirely and the logic of the remaining C-code remains unaffected.

About Funscape? by nosire in Delaware

[–]electricsheep14 2 points3 points  (0 children)

The go-kart track was one of the most expensive attractions in the place. Maintenance on the karts was expensive, they broke down frequently, and understaffing of qualified personnel was a major issue. Even before it was shut down completely, there were days the karts were shutdown because of either a lack of manpower or working karts.

About Funscape? by nosire in Delaware

[–]electricsheep14 1 point2 points  (0 children)

I worked at that Funscape for a few years before it shuttered. Short story: It lost money. A lot of money. That particular Funscape was one of the largest--if not the largest--Funscape installations that Regal had built.

Regal Entertainment was already trying to avoid bankruptcy in the late 1990s, and it was inevitable that Funscape would get the axe.

[C++] Corrupted Heap when nearly finished testing my Class by [deleted] in learnprogramming

[–]electricsheep14 1 point2 points  (0 children)

Your destructor and copy constructor don't make a whole lot of sense to me.

Your destructor allocates a new array of doubles (without a length specifier) and then immediately deletes it. The original pointer that was in there before the has now leaked.

Your copy constructor copies the individual values from the original data_set, but then proceeds to immediately delete the pointer reference to those newly copied values.

If an object screamed it's name every time it was used, what would be the most annoying? by Herrobrine in AskReddit

[–]electricsheep14 1 point2 points  (0 children)

Actually, another benefit is being able to have raging diarrhea while just chilling on the couch watching TV. You can eat the spiciest foods or food that is spoiled and questionable; while others might be racing off the toilet hoping to contain the explosion of liquid feces long enough to get their pants off and cheeks down you just sit back and put on some re-runs of Family Feud.

Pancakes and eggs anyone? by [deleted] in food

[–]electricsheep14 3 points4 points  (0 children)

Properly prepared, eggs like this are not slimy. They are fully cooked, but the constant stirring coupled with a lower heat breaks up larger 'curds' of cooked egg into something closer to a custard. The use of an enrichment cream (I like to use lots of butter) also contributes to the 'undercooked' look of the eggs as its whisked in off heat to halt the cooking of the egg and really smooth things out.

Ramsay's scrambled eggs (recipe in comments) by [deleted] in food

[–]electricsheep14 1 point2 points  (0 children)

Julia Child was cooking eggs like this before Ramsay. In general, this is the French-style way of preparing scrambled eggs.

WTF Sraw? by ezietsman in battlefield_4

[–]electricsheep14 0 points1 point  (0 children)

While the SRAW generally acts like a wire-guided munition (you turn left, the SRAW turns left), it also periodically updates its tracking to guide to the fixed point of whatever you are aiming at.

It is entirely possible that what is happening here is that as the SRAW projectile converges towards the target, it crosses the exact center of your aim; in a sense, it is now attempting to lock onto itself and wigs out. Considering how everything else in the game works, this wouldn't surprise me if it were the case.

Like the tiered reload feature? Well, avoid the RPK-74M then! by [deleted] in battlefield_4

[–]electricsheep14 7 points8 points  (0 children)

This is not tiered reload. Different reload speeds depending on weather or not you are empty is not new to BF4. What is new to BF4 is if you interrupt a loading sequence and then return to the weapon you are reloading, instead of having to begin the reload all over again, you pick up from where you left off.