How to convert NSNumber objects for computational purposes? - nsnumber

I an developing some code in which I use a scanner to get to NSNumbers from a string, say x and y.
Now I want to compute something simple from x and y, say, z = 10.0/(x + y/60.0)/60.0). I can't do this directly, since the compiler doesn't like ordinary arithmetic symbology applied to number objects.
So, I tried defining xD and yD, of type double, and then tried a type conversion
xD = (double) x;
yD = (double) y;
but that also gives a compile error. Just how does one get NSNumber objects converted to be used in ordinary arithmetic expressions? I did considerable browsing of the literature, and didn't find the answer.
Thanks in advance for any help.
John Doner

Use the "value" methods of the NSNumber class such as doubleValue.
// Initialized to something earlier
NSNumber * x, y;
//Convert NSNumbers to double
double xD = [x doubleValue];
double yD = [y doubleValue];
// You can now use regular arithmetic operators
double zD = 10.0 / ((x + y / 60.0) / 60.0);

Related

How to use Wolfram Alpha to find a set of solutions that hold for any variables in the domain

Say that x and y are real numbers and y > 0. And say that I want to find for which values of A do (A + x + y > 0) and (A + x - y > 0) always hold, as long as x, y are in the domain.
How would I specify that on Wolfram Alpha? (Note: obviously these equations have no solution, but I just used it as an example.)
Or, if not on Wolfram, what software/website could I use?
I tried to write: solve for A: [input my first equation], y>0
but that didn't work, as it only gave integer solutions for when A, x, and y vary, instead of finding values of A such that it always holds no matter what x, y are.
https://www.wolframalpha.com/input?i=%28A+%2B+x+%2B+y+%3E+0%29+and+%28A+%2B+x+-+y+%3E+0%29+
[x>-A, -A - x<y<A + x]

Sml program -> confusion on "AS syntax error"

so I have to write a small program in SML ->>
a file named ‘p0.sml’ that contains a function named epoly, which accepts as parameters a list of real values a0 through an, and a single real value x. The list contains the coefficients of a polynomial of the form a0 + a1x + a2x 2 + … + anx n, where the real x used is the x parameter passed to your function. Your implementation must accept the list of coefficients as the first parameter and the value of x as the second. Your function must return the value of the polynomial specified by the parameters passed to it.
this is what I have so far but it won't compile because of a syntax error with as. "Error: syntax error found at AS". If you have any pointers that would be greatly appreciated.
fun epoly([], x:real) = 0.0
= epoly(L:real list as h::T, x:real) = h + (x * epoly(T, x));
It looks like you have a typo. Your second = should be a |.
fun epoly([], x:real) = 0.0
| epoly(L:real list as h::T, x:real) =
h + (x * epoly(T, x));
There is, further, no need to specify types. Your SML compiler can infer the types from data presented. Along with removing unnecessary bindings, this can be reduced to:
fun epoly([], _) = 0.0
| epoly(h::T, x) =
h + (x * epoly(T, x));
From fun epoly([], _) = 0.0 we know epoly will take a tuple of a list and some type and return real.
From:
| epoly(h::T, x) =
h + (x * epoly(T, x));
We know that x is being multiplied by a real, so x must be real. And since h is being added to a real, it must be a real, so the entire list is a real list.
Thus the type of epoly can be inferred correctly to be real list * real -> real.

Nest let syntax from ocaml

I'm just starting to learn Rust and I'm coming from an OCaml background.
One thing that I often use is nested let expressions, in order to group related code together.
let x =
let y = 42 in
y + 1
in
...
My naive translation of this to rust would be:
let x =
let y = 42;
y + 1
;
However this fails to compile.
Is this type of syntax possible, or is it heavily frowned upon and thus is not supported?
Rust uses curly braces to delimit scopes. This works fine:
let x = {
let y = 42;
y + 1
};

Matematica. Residue integral

I just started to use Mathematica and wanted to try out if I got a result correct from a residue integral i made. I have two poles in the UHP, actually looking more complicated but I just wanted to see if Mathematica could to this. This is my code:
x1 = Iw;
x2 = 2 Iw;
Integrate [e^(Ixt)/((x - x1) (x - x2)), {x, -infinity, infinity}, Assumptions -> t > 0]
Is this integral possible to do in Mathematica?
Mathematica sees Iw as a two character variable name, but I w as the product of two things.
Mathematica is fanatic about correct capitalization and spelling.
x1=I w; x2=2 I w;
Integrate[E^(I x t)/((x-x1)(x-x2)), {x,-Infinity,Infinity}, Assumptions-> t>0]
returns a large complicated result depending on the signs and whether w might be zero. If you can supply additional domain information the result might be simplified.

Indentation problem in f# (vs2010 beta1)

I'm just learning f# so possibly I'm doing something very silly. Feel free to point me to the relevant documentation, I've searched but wasn't able to find. I am using Visual Studio 2010 beta on windows 7 (.Net 4.0).
Everything is going well with my first f# project. Well.. almost everything. In particular
I'm writing a very simple linear interpolation function, with the following code:
let linterp (x:double) (xvalues:double list) (yvalues:double list) =
let num_els = xvalues.Length
if x <= xvalues.Head then
let result = yvalues.Head
elif x >= (List.rev xvalues).Head then
let result = (List.rev yvalues).Head
else for idx in [0 .. num_els] do
if List.nth xvalues idx >= x then
let x0 = xvalues.Item idx
let y0 = yvalues.Item idx
let x1 = xvalues.Item (idx+1)
let y1 = yvalues.Item (idx+1)
let result = y0 + (y1-y0)/(x1-x0)*(x - x0)
result
and I am receiving a series of errors which completely elude my comprehension.
This is the list of errors and warnings:
"error in the return expression for this 'let'. Possible correct indentation" for the first, second and last "let".
"possible incorrect indentation: this token is offside of context started at position (39:10). Try indenting this token further or using standard formatting conventions " for the "if"
"incomplete structured construct at or before this point in expression" for the last line (result).
I will add that I had to sweat a bit to annotate the types, since for some reason unknown to me the compiler was able to infer correctly for the first list, but for the second the type was always inferred as unit Also, my original version didn't bind the name result, but simply "returned the expression", like in
if x <= xvalues.Head then
yvalues.Head
or in
else for idx in [0 .. num_els] do
if List.nth xvalues idx >= x then
let x0 = xvalues.Item idx
let y0 = yvalues.Item idx
let x1 = xvalues.Item (idx+1)
let y1 = yvalues.Item (idx+1)
y0 + (y1-y0)/(x1-x0)*(x - x0)
This leaves an error under the "for", saying that "This expression has type unit but is here used with type double" and that the "if" is possibly incorrectly indented.
I think that when I'll see the solution to this I'll feel silly, but I've been stuck on such a simple problem for more than an hour, so I am asking for your help.
Thanks in advance!
ps: I've checked that the tabs are correctly interpreted as spaces in the Tools->options->.... -> F#->Tabs menu
pps: this is my first question on SO :-)
Your problem is that something like
let result = yvalues.Head
is not a complete expression, and so it can't form the body of one of the branches of an if block. Your initial approach was correct, except that a for ... do loop doesn't return a meaningful value (it returns (), which is the only value of type unit, as the compiler is trying to explain; this is similar to void in languages like C#). Instead of the for loop, you'll want to use an expression that has the value you're looking for. The smallest change to your code would be to use a mutable value which you imperatively set in the loop. A more idiomatic approach would be to use a built in function like List.fold to condense the list to a single value. This is complicated by the fact that you need to access consecutive entries in your lists (and that you need to operate on both xvalues and yvalues simulataneously), which means you'd probably need to use List.zip and Seq.pairwise, which might reduce clarity for someone not used to F#.
Additionally, there are a few other changes you can apply to make your code more idiomatic. For instance, let x0 = xvalues.Item idx would more commonly be written let x0 = xvalues.[idx]. However, note that F# lists are immutable, linked lists, and therefore do not support fast random access. This is another reason to favor an approach that uses the built-in List operators.
let linterp x (xvalues:double list) (yvalues:double list) =
if x <= xvalues.Head then
yvalues.Head
elif x >= (List.rev xvalues).Head then
(List.rev yvalues).Head
else
let idx = List.findIndex (fun e -> e >= x) xvalues
let x0 = xvalues.Item idx
let y0 = yvalues.Item idx
let x1 = xvalues.Item (idx+1)
let y1 = yvalues.Item (idx+1)
y0 + (y1-y0)/(x1-x0)*(x - x0)

Resources