AVAudio, random crashes - avaudioplayer

Here is a strange issue I am having with sound in an iOS app.
Since my code works about 90% of the time, it is not that easy to reproduce the bug.
But the problem seems to happen (once in a while) at the end of a recording; either when I stop the recording manually or when it stops automatically.
Under the debugger I see the crash showing up at a point in the program related to the plugging in/out of an earphone.
In reality there is no earphone plugged in and I am not plugging in or out any earphone at all.
I have this code at some point in my program:
AudioSessionPropertyID routeChangeID=kAudioSessionProperty_AudioRouteChange;
AudioSessionAddPropertyListener(routeChangeID,svcHandleRouteChange,(__bridge void *)(self));
and this:
void svcHandleRouteChange(void *inUserData,AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,const void *inPropertyValue)
{
CFStringRef route;
UInt32 propertySize=sizeof(CFStringRef);
if (AudioSessionGetProperty(kAudioSessionProperty_AudioRoute,&propertySize,&route)==0) {
NSString *routeString = (__bridge NSString *) route;
if ([routeString isEqualToString: #"Headphone"]) {
// A headphone is now plugged in.
// We do nothing.
} else if ([routeString isEqualToString: #"Speaker"]) {
// No headphone is plugged in. The device is using the speaker.
LocalViewController* this=(__bridge LocalViewController*)inUserData;
//[this->audioPlayer pause]; // The private variable audioPlayer could be accessed like this.
[this headPhoneOut];
} else {
// This should not happen!!
}
}
}
The crash is happening in the line:
LocalViewController* this=(__bridge LocalViewController*)inUserData;
And finally here is the backtrace I get in the debugger after the crash in case that may help someone to give me a hint:
(lldb) bt
* thread #5: tid = 0x292d8, 0x3abba0ec libobjc.A.dylib`objc_retain + 12, stop reason = EXC_BAD_ACCESS (code=1, address=0x6e966a29)
frame #0: 0x3abba0ec libobjc.A.dylib`objc_retain + 12
frame #1: 0x000eb7d6 parl100`svcHandleRouteChange(inUserData=0x14e5f190, inPropertyID=1919902568, inPropertyValueSize=4, inPropertyValue=0x14f0b120) + 182 at LocalViewController.m:801
frame #2: 0x301ae944 AudioToolbox`AudioSessionPropertyListeners::CallPropertyListenersImp(unsigned long, unsigned long, void const*) + 684
frame #3: 0x301ae456 AudioToolbox`AudioSessionPropertyListeners::CallPropertyListeners(unsigned long, unsigned long, void const*) + 334
frame #4: 0x301aee90 AudioToolbox`HandleCFPropertyListChange(unsigned int, unsigned long, unsigned char*, unsigned int) + 412
frame #5: 0x301aec7e AudioToolbox`HandleAudioSessionPropertyChangedBlobListenerMessage(unsigned int, void*, unsigned int) + 206
frame #6: 0x301aeb0e AudioToolbox`ASCallbackReceiver_AudioSessionPropertyChangedBlobListenerMessage + 178
frame #7: 0x301aea3e AudioToolbox`_XAudioSessionPropertyChangedBlobListenerMessage + 94
frame #8: 0x301ae088 AudioToolbox`mshMIGPerform + 124
frame #9: 0x308dc9e6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
frame #10: 0x308dc982 CoreFoundation`__CFRunLoopDoSource1 + 346
frame #11: 0x308db156 CoreFoundation`__CFRunLoopRun + 1398
frame #12: 0x30845ce6 CoreFoundation`CFRunLoopRunSpecific + 522
frame #13: 0x30845aca CoreFoundation`CFRunLoopRunInMode + 106
frame #14: 0x2f837588 libAVFAudio.dylib`GenericRunLoopThread::Entry(void*) + 128
frame #15: 0x2f82ba96 libAVFAudio.dylib`CAPThread::Entry(CAPThread*) + 178
frame #16: 0x3b1e4c1c libsystem_pthread.dylib`_pthread_body + 140
frame #17: 0x3b1e4b8e libsystem_pthread.dylib`_pthread_start + 102
(lldb)
Here is a second case where the same problem happens at a different point in the app:
2013-10-23 12:25:12.135 parl100[1532:60b] audioRecorderDidFinishRecording
(lldb) bt
* thread #5: tid = 0x2b37a, 0x3abba0ec libobjc.A.dylib`objc_retain + 12, stop reason = EXC_BAD_ACCESS (code=1, address=0xd0003511)
frame #0: 0x3abba0ec libobjc.A.dylib`objc_retain + 12
frame #1: 0x000f18a2 parl100`cvcHandleRouteChange(inUserData=0x176b5610, inPropertyID=1919902568, inPropertyValueSize=4, inPropertyValue=0x1762d660) + 182 at ZapViewController.m:566
frame #2: 0x301ae944 AudioToolbox`AudioSessionPropertyListeners::CallPropertyListenersImp(unsigned long, unsigned long, void const*) + 684
frame #3: 0x301ae456 AudioToolbox`AudioSessionPropertyListeners::CallPropertyListeners(unsigned long, unsigned long, void const*) + 334
frame #4: 0x301aee90 AudioToolbox`HandleCFPropertyListChange(unsigned int, unsigned long, unsigned char*, unsigned int) + 412
frame #5: 0x301aec7e AudioToolbox`HandleAudioSessionPropertyChangedBlobListenerMessage(unsigned int, void*, unsigned int) + 206
frame #6: 0x301aeb0e AudioToolbox`ASCallbackReceiver_AudioSessionPropertyChangedBlobListenerMessage + 178
frame #7: 0x301aea3e AudioToolbox`_XAudioSessionPropertyChangedBlobListenerMessage + 94
frame #8: 0x301ae088 AudioToolbox`mshMIGPerform + 124
frame #9: 0x308dc9e6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
frame #10: 0x308dc982 CoreFoundation`__CFRunLoopDoSource1 + 346
frame #11: 0x308db156 CoreFoundation`__CFRunLoopRun + 1398
frame #12: 0x30845ce6 CoreFoundation`CFRunLoopRunSpecific + 522
frame #13: 0x30845aca CoreFoundation`CFRunLoopRunInMode + 106
frame #14: 0x2f837588 libAVFAudio.dylib`GenericRunLoopThread::Entry(void*) + 128
frame #15: 0x2f82ba96 libAVFAudio.dylib`CAPThread::Entry(CAPThread*) + 178
frame #16: 0x3b1e4c1c libsystem_pthread.dylib`_pthread_body + 140
frame #17: 0x3b1e4b8e libsystem_pthread.dylib`_pthread_start + 102
(lldb)
I can provide more code if necessary. Just tell me what I should include.
I hope someone can point me to something I must not be doing the proper way.

Related

EXC_BAD_ACCESS with Metal Sampler State

So I'm getting the annoying EXC_BAD_ACCESS error...
With zombies I see that it's the MTLSamplerState that has to do with the crash:
*** -[MTLDebugSamplerState retain]: message sent to deallocated instance 0x1c9065c40
This is how I declare it:
let sampler_descriptor = MTLSamplerDescriptor()
sampler_descriptor.minFilter = .linear
sampler_descriptor.magFilter = .linear
sampler_descriptor.sAddressMode = .clampToZero
sampler_descriptor.tAddressMode = .clampToZero
let sampler_state = self.device!.makeSamplerState(descriptor: sampler_descriptor)!
I later in the same function, where the error happens, I access the sampler like this:
renderCommandEncoder.setFragmentSamplerState(sampler_state, index: 0)
The crash happens randomly at different times in my program, but not always, not sure how to debug in further...
Why would sampler_state be deallocated? Isn't Swift suppose to do ARC?
Update:
Here's the stack trace:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18339161c)
frame #0: 0x000000018339161c CoreFoundation`___forwarding___ + 1180
frame #1: 0x00000001832770dc CoreFoundation`_CF_forwarding_prep_0 + 92
* frame #2: 0x00000001020c2b14 Test App`MetalView.render(view=0x0000000105111000, self=0x0000000105111000) at MetalView.swift:378
frame #3: 0x00000001020c0d38 Test App`closure #1 in MetalView.draw(rect=(origin = (x = 0, y = 0), size = (width = 112.5, height = 112.5)), self=0x0000000105111000) at MetalView.swift:238
frame #4: 0x00000001020c36dc Test App`thunk for #callee_owned () -> (#error #owned Error) at MetalView.swift:0
frame #5: 0x00000001020c6ff0 Test App`partial apply for thunk for #callee_owned () -> (#error #owned Error) at MetalView.swift:0
frame #6: 0x0000000103c24e1c libswiftObjectiveC.dylib`ObjectiveC.autoreleasepool<A>(invoking: () throws -> A) throws -> A + 68
frame #7: 0x00000001020c0c0c Test App`MetalView.draw(rect=(origin = (x = 0, y = 0), size = (width = 112.5, height = 112.5)), self=0x0000000105111000) at MetalView.swift:240
frame #8: 0x00000001020c3798 Test App`#objc MetalView.draw(_:) at MetalView.swift:0
frame #9: 0x0000000196844ed0 MetalKit`-[MTKView draw] + 140
frame #10: 0x0000000187368010 QuartzCore`-[CALayer display] + 188
frame #11: 0x00000001872dbfdc QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 520
frame #12: 0x0000000187302340 QuartzCore`CA::Transaction::commit() + 540
frame #13: 0x00000001872570d4 QuartzCore`CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 928
frame #14: 0x0000000187307fa4 QuartzCore`display_timer_callback(__CFMachPort*, void*, long, void*) + 236
frame #15: 0x000000018331a080 CoreFoundation`__CFMachPortPerform + 188
frame #16: 0x00000001833349dc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
frame #17: 0x00000001833340e0 CoreFoundation`__CFRunLoopDoSource1 + 440
frame #18: 0x0000000183331bb4 CoreFoundation`__CFRunLoopRun + 2196
frame #19: 0x00000001832522d8 CoreFoundation`CFRunLoopRunSpecific + 436
frame #20: 0x00000001850e3f84 GraphicsServices`GSEventRunModal + 100
frame #21: 0x000000018c7ff880 UIKit`UIApplicationMain + 208
frame #22: 0x000000010231a1e8 Test App`main at AppDelegate.swift:13
frame #23: 0x0000000182d7656c libdyld.dylib`start + 4

OpenCL oil painting

I want to implement oil painting filter in OpenCL,but the output image is always black and I cannot figure out why.
Here's the kernel code:
__kernel void oil_painting(__global const char* R,__global const char* G,__global const char* B,
__global char* r,__global char* g,__global char* b)
{
int i=get_global_id(0);
int j=get_global_id(1);
int i1,j1,k;
int avgR[256],avgG[256],avgB[256],intensity_count[256];
int max_pixels=0,max_intensity=0,current_intensity;
for (i1=0;i1<4;i1++) {
for (j1=0;j1<4;j1++) {
current_intensity=(((R[(i+i1)*512+j+j1]+
G[(i+i1)*512+j+j1]+
B[(i+i1)*512+j+j1])/3)*70)/255;
intensity_count[current_intensity]++;
if (intensity_count[current_intensity]>max_pixels) {
max_pixels=intensity_count[current_intensity];
max_intensity=current_intensity;
}
avgR[current_intensity]+=R[(i+i1)*512+j+j1];
avgG[current_intensity]+=G[(i+i1)*512+j+j1];
avgB[current_intensity]+=B[(i+i1)*512+j+j1];
}
}
r[i*512+j]=min(255,max(0,avgR[max_intensity]/max_pixels));
g[i*512+j]=min(255,max(0,avgG[max_intensity]/max_pixels));
b[i*512+j]=min(255,max(0,avgB[max_intensity]/max_pixels));
}
Code snippets like the following are going to get you into a lot of trouble:
current_intensity=(((R[(i+i1)*512+j+j1]+
G[(i+i1)*512+j+j1]+
B[(i+i1)*512+j+j1])/3)*70)/255;
Consider what happens for a pixel of <127,127,127>:
127 + 127 + 127 = 125 (truncated because `char` is only 8 bytes...)
125 / 3 = 41
41 * 70 = 54 (truncated because `char` is only 8 bytes...)
54 / 255 = 0 (this will always equal 0!)
So intensity_count will only ever have its 0-th index incremented, and nothing else.
Casting everything to int might fix this problem.
current_intensity=((((int)R[(i+i1)*512+j+j1]+
(int)G[(i+i1)*512+j+j1]+
(int)B[(i+i1)*512+j+j1])/3)*70)/255;
New output:
127 + 127 + 127 = 381
381 / 3 = 127
127 * 70 = 8890
8890 / 255 = 34
But you've now got a new problem: what if the values are any higher than 127? Suppose we change this to use <200, 200, 200> instead?
-56 + -56 + -56 = -168 (`char` only has a range in [-128, 127]! You're overflowing!)
-168 / 3 = -56
-56 * 70 = -3920
-3920 / 255 = -15
And now you've crashed your program because either you're going to attempt to access index -15, which is illegal, or you're going to attempt to access index 2^64 - 15 - 1, which is going to still be illegal. Either way, you're going to get bad results.
The simplest solution is to change your kernel arguments to global uchar * instead of global char *, and then make sure that any and all arithmetic is casted upwards to int or long to ensure that overflow doesn't take place.

OpenCL Intel Iris Integrated Graphics exits with Abort Trap 6: Timeout Issue

I am attempting to write a program that executes Monte Carlo simulations using OpenCL. I have run into an issue involving exponentials. When the value of the variable steps becomes large, approximately 20000, the calculation of the exponent fails unexpectedly, and the program quits with "Abort Trap: 6". This seems to be a bizarre error given that steps should not affect memory allocation. I have tried setting normal, alpha, and beta to 0 but this does not resolve the problem however commenting out the exponent and replacing it with the constant 1 seems to fix the problem. I have run my code on an AWS GPU instance and it does not run into any issues. Does anybody have any ideas as to why this might be a problem on an integrated graphics card?
SOLUTION
Execute the kernel multiple times over a smaller ranges to keep kernel execution time under 5 seconds
Code Snippet
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static uint MWC64X(uint2 *state) {
enum { A = 4294883355U };
uint x = (*state).x, c = (*state).y;
uint res = x ^ c;
uint hi = mul_hi(x, A);
x = x * A + c;
c = hi + (x < c);
*state = (uint2)(x, c);
return res;
}
__kernel void discreteMonteCarloKernel(...) {
float cumulativeWalk = stockPrice;
float currentValue = stockPrice;
...
uint n = get_global_id(0);
uint2 seed2 = (uint2)(n, seed);
uint random1 = MWC64X(&seed2);
uint2 seed3 = (uint2)(random1, seed);
uint random2 = MWC64X(&seed3);
float alpha = (interestRate - 0.5 * sigma * sigma) * dt;
float beta = sigma * sqrt(dt);
float u1;
float u2;
float a;
float b;
float normal;
for (int j = 0; j < steps; j++) {
random1 = MWC64X(&seed2);
if (random1 == 0) {
random1 = MWC64X(&seed2);
}
random2 = MWC64X(&seed3);
u1 = (float)random1 / (float)0xffffffff;
u2 = (float)random2 / (float)0xffffffff;
a = sqrt(-2 * log(u1));
b = 2 * M_PI * u2;
normal = a * sin(b);
exponent = exp(alpha + beta * normal);
currentValue = currentValue * exponent;
cumulativeWalk += currentValue;
...
}
Problem Report
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Application Specific Information:
abort() called
Application Specific Signatures:
Graphics hardware encountered an error and was reset: 0x00000813
Thread 0 Crashed:: Dispatch queue: opencl_runtime
0 libsystem_kernel.dylib 0x00007fffb14bad42 __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fffb15a85bf pthread_kill + 90
2 libsystem_c.dylib 0x00007fffb1420420 abort + 129
3 libGPUSupportMercury.dylib 0x00007fffa98e6fbf gpusGenerateCrashLog + 158
4 com.apple.driver.AppleIntelHD5000GraphicsGLDriver 0x000000010915f13b gpusKillClientExt + 9
5 libGPUSupportMercury.dylib 0x00007fffa98e7983 gpusQueueSubmitDataBuffers + 168
6 com.apple.driver.AppleIntelHD5000GraphicsGLDriver 0x00000001091aa031 IntelCLCommandBuffer::getNew(GLDQueueRec*) + 31
7 com.apple.driver.AppleIntelHD5000GraphicsGLDriver 0x00000001091a9f99 intelSubmitCLCommands(GLDQueueRec*, unsigned int) + 65
8 com.apple.driver.AppleIntelHD5000GraphicsGLDriver 0x00000001091b00a1 CHAL_INTEL::ChalContext::ChalFlush() + 83
9 com.apple.driver.AppleIntelHD5000GraphicsGLDriver 0x00000001091aa2c3 gldFinishQueue + 43
10 com.apple.opencl 0x00007fff9ffeeb37 0x7fff9ffed000 + 6967
11 com.apple.opencl 0x00007fff9ffef000 0x7fff9ffed000 + 8192
12 com.apple.opencl 0x00007fffa000ccca 0x7fff9ffed000 + 130250
13 com.apple.opencl 0x00007fffa001029d 0x7fff9ffed000 + 144029
14 libdispatch.dylib 0x00007fffb13568fc _dispatch_client_callout + 8
15 libdispatch.dylib 0x00007fffb1357536 _dispatch_barrier_sync_f_invoke + 83
16 com.apple.opencl 0x00007fffa001011d 0x7fff9ffed000 + 143645
17 com.apple.opencl 0x00007fffa000bda6 0x7fff9ffed000 + 126374
18 com.apple.opencl 0x00007fffa00011df clEnqueueReadBuffer + 813
19 simplisticComparison 0x0000000107b953cf BinomialMultiplication::execute(int) + 1791
20 simplisticComparison 0x0000000107b9ec7f main + 767
21 libdyld.dylib 0x00007fffb138c235 start + 1
Thread 1:
0 libsystem_pthread.dylib 0x00007fffb15a50e4 start_wqthread + 0
1 ??? 0x000070000eed6b30 0 + 123145552751408
Thread 2:
0 libsystem_pthread.dylib 0x00007fffb15a50e4 start_wqthread + 0
Thread 3:
0 libsystem_pthread.dylib 0x00007fffb15a50e4 start_wqthread + 0
1 ??? 0x007865646e496d65 0 + 33888479226719589
Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000000 rbx: 0x0000000000000006 rcx: 0x00007fff58074078 rdx: 0x0000000000000000
rdi: 0x0000000000000307 rsi: 0x0000000000000006 rbp: 0x00007fff580740a0 rsp: 0x00007fff58074078
r8: 0x0000000000000000 r9: 0x00007fffb140ba50 r10: 0x0000000008000000 r11: 0x0000000000000206
r12: 0x00007f92de80a7e0 r13: 0x00007f92e0008c00 r14: 0x00007fffba29e3c0 r15: 0x00007f92de801a00
rip: 0x00007fffb14bad42 rfl: 0x0000000000000206 cr2: 0x00007fffba280128
Logical CPU: 0
Error Code: 0x02000148
Trap Number: 133
I have a guess. The driver can crash in two ways:
We reference a bad buffer address. This is probably not your case.
We time out (exceed the TDR). A kernel has a few seconds to complete.
My money is on #2. If the larger value (steps) makes the GPU run too long, the system will kill things.
I am not familiar with the guts of Apple's Intel driver, but typically there is a way to disable the TDR in extreme cases. E.g. see the Windows Documenation on TDRs to get the gist. (Linux drivers have a way to disable this too.)
Normally we want to avoid running things that take super long and it might be a good idea to decompose the workload in some way so that you naturally don't hit this kill switch. E.g. perhaps chunk the "steps" into smaller chunks (pass in and save your state for parts you can't recompute).

How to print output of .bas file to text

I am trying to print coordinate outputs of a program to a text file in order to use it in another program but I don't really know anything about GWBASIC and its my first time using MS-DOS. I need it to open a text file named plot.txt and print output there and save it without actually plotting on GWBASIC. Here is the program which I found in an old magazine.
810 REM MAKE A GLOBULAR
12 REM
14 R0=20: R2=R0*R0: R3=R2*R0
16 P1=3.14159265#
18 C0=P1*P1*R3/4
20 R1=R0/SQR(2)
22 XM=512: YM=512
24 X2=XM/2: Y2=YM/2: S=5
26 INPUT "HOW MANY STARS ";T
27 RANDOMIZE TIMER
28 CLS: REM CLEAR SCREEN
30 FOR I=1 TO T
32 C=C0*RND: R=R1
34 REM
36 REM NOW FIND R
38 FOR K=1 TO 5
40 GOSUB 100
42 R=R+(C-C1)/D
44 NEXT K
46 REM 3-DIMENSIONAL PLACE
48 X=RND-.5
50 Y=RND-.5
52 Z=RND-.5
54 S1=SQR(X*X+Y*Y+Z*Z)
56 IF S1>.5 THEN GOTO 48
58 REM POINT IS NOW IN SPHERE
60 R=R*S1: X=X*R: Y=Y*R: Z=Z*R
62 GOSUB 200
64 NEXT I
66 END
68 REM
100 REM NEWTON-RAPHSON ITERATION
105 A=R/R0
110 C1=ATN(A)*.5*R3
115 A=1+A*A
120 C1=C1+R*.5*R2/A
125 C1=P1*(C1-R*R2/(A*A))
130 D=4*P1*R*R/(A*A*A)
135 RETURN
140 REM
200 REM 2-DIMENSIONAL PLOT
203 SCREEN 9
205 X=X*S+X2: Y=Y*S+Y2
210 IF X<0 OR Y<0 THEN 225
215 IF X>=XM OR Y>=YM THEN 225
220 PSET(X,Y)
225 RETURN
230 REM ------------------------
240 REM APPEARED IN ASTRONOMICAL
250 REM COMPUTING, SKY & TELE-
260 REM SCOPE, APRIL, 1986
270 REM ------------------------
Here is a Python 3 paraphrase:
#globular.py
#Python paraphrase of model.bas from
#http://www.skyandtelescope.com/wp-content/uploads/model.bas
from math import pi, sqrt, atan
from random import uniform, random
#Global variables:
r0 = 20.0
r2 = r0**2
r3 = r0**3
c0 = pi**2*r3/4
r1 = r0/sqrt(2)
def NRI(c,r):
#Newton-Raphson Iteration
a = r/r0
c1 = atan(a)*0.5*r3
a = 1+a**2
c1 += r*0.5*r2/a
c1 = pi*(c1-r*r2/a**2)
d = 4*pi*r**2/a**3
return (c1,d)
def makeStars(t):
stars = []
for i in range(t):
c = c0*random()
r = r1
for k in range(5):
c1,d = NRI(c,r)
r += (c-c1)/d
while True:
x = uniform(-0.5,0.5)
y = uniform(-0.5,0.5)
z = uniform(-0.5,0.5)
s1 = sqrt(x**2 + y**2 + z**2)
if s1 <= 0.5: break
r *= s1
x *= r
y *= r
z *= r
stars.append((x,y,z))
return stars
def starsToFile(t,fname):
stars = makeStars(t)
f = open(fname,'w')
for star in stars:
print(*star, sep = ', ',file = f)
f.close()
I skipped the part about printing x and y and instead wrote a function makeStars to return a list of (x,y,z) tuples, as well as a related function which takes such an output and sends it to a text file. This last function is the only thing that used Python 3 instead of Python 2. If you are using Python 2 you can import Python 3's print function from the future.
Typing starsToFile(100,'stars.txt') in the Python shell gave me a text file which begins:
-0.32838465248713156, -0.3294895266926551, -1.2963580524762535
14.20224408569865, 1.4434961933043464, 6.450969593697097
1.6525937589658193, -0.24447292610082685, 1.0543647986350608
1.5707528567123823, 5.190972598268825, -2.0054790217091134
I don't have good 3-d scatter-plot graphing at my finger tips, but here is a screen shot of 50 points generated by the function and plotted using a computer algebra system called Derive:
Final remark: I wonder if there is a typo in the source code. The line
C0=P1*P1*R3/4
strikes me as suspicious since it is fairly rare in mathematics for pi to appear squared -- though it does happen. Maybe there should be only 1 factor of pi there (which would then have the effect of setting C0 proportional to the volume of the sphere of radius R0). On the other hand, I don't know exactly what is happening here, so I left it in. If the results seem problematic, you could maybe experiment with that line.
If you want a copy of the calculated coordinates simply add these lines:
1 OPEN "PLOT.TXT" FOR OUTPUT AS #1
65 CLOSE #1
221 PRINT #1, X + "," + Y
The program will work as before but in addition to this it outputs the coordinate to a file named plot.txt
Put them in an image with 640x350 size (that size is demanded by SCREEN 9) and you get the same result.

How to find x mod 15 without using any Arithmetic Operations?

We are given a unsigned integer, suppose. And without using any arithmetic operators ie + - / * or %, we are to find x mod 15. We may use binary bit manipulations.
As far as I could go, I got this based on 2 points.
a = a mod 15 = a mod 16 for a<15
Let a = x mod 15
then a = x - 15k (for some non-negative k).
ie a = x - 16k + k...
ie a mod 16 = ( x mod 16 + k mod 16 ) mod 16
ie a mod 15 = ( x mod 16 + k mod 16 ) mod 16
ie a = ( x mod 16 + k mod 16 ) mod 16
OK. Now to implement this. A mod16 operations is basically & OxF. and k is basically x>>4
So a = ( x & OxF + (x>>4) & OxF ) & OxF.
It boils down to adding 2 4-bit numbers. Which can be done by bit expressions.
sum[0] = a[0] ^ b[0]
sum[1] = a[1] ^ b[1] ^ (a[0] & b[0])
...
and so on
This seems like cheating to me. I'm hoping for a more elegant solution
This reminds me of an old trick from base 10 called "casting out the 9s". This was used for checking the result of large sums performed by hand.
In this case 123 mod 9 = 1 + 2 + 3 mod 9 = 6.
This happens because 9 is one less than the base of the digits (10). (Proof omitted ;) )
So considering the number in base 16 (Hex). you should be able to do:
0xABCE123 mod 0xF = (0xA + 0xB + 0xC + 0xD + 0xE + 0x1 + 0x2 + 0x3 ) mod 0xF
= 0x42 mod 0xF
= 0x6
Now you'll still need to do some magic to make the additions disappear. But it gives the right answer.
UPDATE:
Heres a complete implementation in C++. The f lookup table takes pairs of digits to their sum mod 15. (which is the same as the byte mod 15). We then repack these results and reapply on half as much data each round.
#include <iostream>
uint8_t f[256]={
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,1,
2,3,4,5,6,7,8,9,10,11,12,13,14,0,1,2,
3,4,5,6,7,8,9,10,11,12,13,14,0,1,2,3,
4,5,6,7,8,9,10,11,12,13,14,0,1,2,3,4,
5,6,7,8,9,10,11,12,13,14,0,1,2,3,4,5,
6,7,8,9,10,11,12,13,14,0,1,2,3,4,5,6,
7,8,9,10,11,12,13,14,0,1,2,3,4,5,6,7,
8,9,10,11,12,13,14,0,1,2,3,4,5,6,7,8,
9,10,11,12,13,14,0,1,2,3,4,5,6,7,8,9,
10,11,12,13,14,0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,0,1,2,3,4,5,6,7,8,9,10,11,
12,13,14,0,1,2,3,4,5,6,7,8,9,10,11,12,
13,14,0,1,2,3,4,5,6,7,8,9,10,11,12,13,
14,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0};
uint64_t mod15( uint64_t in_v )
{
uint8_t * in = (uint8_t*)&in_v;
// 12 34 56 78 12 34 56 78 => aa bb cc dd
in[0] = f[in[0]] | (f[in[1]]<<4);
in[1] = f[in[2]] | (f[in[3]]<<4);
in[2] = f[in[4]] | (f[in[5]]<<4);
in[3] = f[in[6]] | (f[in[7]]<<4);
// aa bb cc dd => AA BB
in[0] = f[in[0]] | (f[in[1]]<<4);
in[1] = f[in[2]] | (f[in[3]]<<4);
// AA BB => DD
in[0] = f[in[0]] | (f[in[1]]<<4);
// DD => D
return f[in[0]];
}
int main()
{
uint64_t x = 12313231;
std::cout<< mod15(x)<<" "<< (x%15)<<std::endl;
}
Your logic is somewhere flawed but I can't put a finger on it. Think about it yourself, your final formula operates on first 8 bits and ignores the rest. That could only be valid if the part you throw away (9+ bits) are always the multiplication of 15. However, in reality (in binary numbers) 9+ bits are always multiplications of 16 but not 15. For example try putting 1 0000 0000 and 11 0000 0000 in your formula. Your formula will give 0 as a result for both cases, while in reality the answer is 1 and 3.
In essense I'm almost sure that your task can not be solved without loops. And if you are allowed to use loops - then it's nothing easier than to implement bitwiseAdd function and do whatever you like with it.
Added:
Found your problem. Here it is:
... a = x - 15k (for some non-negative k).
... and k is basically x>>4
It equals x>>4 only by pure coincidence for some numbers. Take any big example, for instance x=11110000. By your calculation k = 15, while in reality it is k=16: 16*15 = 11110000.

Resources