Tclsh precision issue with multiple and devide - precision

I met a problem in tclsh.
set a [expr 0.009 *10000 *0.0001]
%> 0.009000000000000001
I know the result probably caused by "IEEE Standard for Binary Floating-Point Arithmetic".
but the question is, why I changed the sentence " *0.0001" to "/10000".
set a [expr 0.009*10000/10000.0]
%> 0.009
Then, the answer is correct.
Could anyone please explain the reason for me?
Thanks!

Related

How to format a number with XPath in Tibco BW 5

I've managed to format the following lines in XPath, from this format:
1000.50
30
to this:
100050
3000
The solution I've adopted is:
concat(substring-before([number], '.'), substring-after([number], '.'))
If the . is not present I directly multiply the number by 100.
I'm wondering if there is any better way to do that. My second thought was using Java.
What goes wrong if you just multiply by 100? So long as the result of multiplying by 100 is an exact integer, it should be formatted without a "." when converted to a string. If there are rounding errors that mean the result is not an exact integer, you might want to use round().
The concat() approach seems fragile to me: what if someone gives you input like 1000.5 or perhaps 1000.500?

Flappy Bird Coding Error in Swift

I've encountered a coding error for my flappy bird project, in Xcode 6.1.
The code is to allow rotation of the bird, and it reads:
bird.zRotation = self.acotarMinMax(-1, max: 0.3, valor: bird.physicsBody?.velocity.dy * (bird.physicsBody?.velocity.dy < 0 ? 0.003 : 0.001))
the error occurs under dy, it reads:
value of optional type 'CGFloat?' not unwrapped; did you mean to use '!' or '?'?
How can I correct the error, or is there another way to generate rotation?
I am a total beginner of Swift, so I'm having a hard time figuring out if this is a syntax problem or something to do with the updated version.
I obtained the code from a online tutorial, and it worked in the video.
Read up on the Swift Optional class: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
So it seems that dy is an Optional value, which means that it either has a value or it does not, but you don't know until you look into it (like Schroedinger's Cat). So if you need this value and you know it will not be nil, you unwrap it by writing bird.physicsBody?.velocity.dy! which will look into the "box" and take the value out but crash if there is nothing inside. If you write dy? it will look into the box but ignore it if the box is empty.
More on Optional Chaining (which is what you do) here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html
So I just figured out my error.
As Sebastian correctly pointed out, it is to do with optional chaining.
The code that worked is:
bird.zRotation = self.acotarMinMax(-1, max: 1, valor: bird.physicsBody!.velocity.dy * (bird.physicsBody!.velocity.dy < 0 ? 0.002 : 0.002))
I used '!' to force a value on physicsBody. The precise mechanism of why this works is still a blur to me. But my guess is to do with the new version's correction method.

Julia doing something strange with assignments

I am trying to learn Julia by repeating some of the easy ProjectEuler problems in Julia. Everything has been really smooth so far, up until I encountered this frustrating problem. I spent some time debugging my code, and here's what I found:
(Hopefully I'm not missing something really stupid here)
function is_abundant(n::Int) #just a function
return prod(map(x->int((x[1]^(x[2]+1)-1)/(x[1]-1)),factor(n))) > 2 * n
end
abundants=[12] #there should be a better way to initialize an Array
for i=13:28120
if is_abundant(i)
push!(abundants,i)
end
end
le=abundants; #The following lines are the problems
ri=abundants;
d=length(abundants)
println(d)
pop!(le)
shift!(ri)
println(le==ri, " ", endof(ri), " ", endof(abundants))
The output I get is:
6964
true 6962 6962
which means that Julia has changed all three sets of le , ri and abundants with each of pop! and shift! commands. I was able to work around this bug/problem by using a dumb extra identity mapping:
le=map(x->x,abundants)
ri=map(x->x,abundants)
Now the output would change to what I initially expected:
6964
false 6963 6964
My question is, if this is not a bug, why is Julia keeping an equivalence relation between le , ri and abundants sets in the first place? Also, can anyone reproduce this behaviour? I am using Julia "Version 0.3.0-rc3+14 (2014-08-13 16:01 UTC)" on Ubuntu 14.04.
le and ri both point to the same list that abundants points to, so this is expected behavior - they are all operating on the same memory. This part of the manual might help you understand. Or possibly the MATLAB differences section, as it is different in MATLAB (but most other languages are like Julia).
For
abundants=[12] #there should be a better way to initialize an Array
how about
abundants = {} # Vector of anything
or
abundants = Int[] # Vector of ints
and instead of your map(x->x,...), you can just use copy.

Crystal Report displaying formula result as 00

First of all I would like to apologize if this has been asked before, I just couldn't find the answer.
To the point:
My problem is as follows: I am using Crystal Reports for VS2010 and I have a field that should dispaly 1 or -1 based on an If check
If IsNull ({Orders.OrderReplacedBy})
Then 1
Else -1
It should display in my report 1 for null values of the field and -1 otherwise. Problem is that in my report I get only .00 no matter the value of the if test.
Furthermore I have another formula field that does of a sum of all the 1s and -1s showed on the report, and it seems to be working a little weird. In my database I have 772 total rows, of which 39 are NULL (so there should be 733 1s and 39 -1s with a sum of 694) and the displayed sum is 488. The code for the sum is as follows:
Sum({#N})
where N is the field where I calculate the 1s and -1s.
What I've tried so far:
changed the If test from IsNull to ToText({Orders.OrderReplacedBy})=""
changed the If test to {Orders.OrderReplacedBy}>0 as the OrderReplacedBy field in the database is numeric and there cannot be values less than zero, so the NULLs should just trigger the Else.
trid to change the display of the formula to "1" and ToNumber("1") nothing seems to work
I honestly have no more ideas of what else to try and I also have no knowledge of Crystal Reports (I am only working with it for 2 days and just need to modify a report on an existing app).
Any help would be appreciated,
Thanks :)
P.S.: I am working with Silverlight 4
Since no one answered I presume that this hasn't happened to anyone or isn't of much interest. But after doing some more research and trying out other stuff I've found a workaroud that I'll post (maybe someone else bumps to this, who knows?):
In the N formula (I don't know why it didn't work the last time I tried) I changed things like this:
If IsNull({Orders.OrderReplacedBy})
Then "1"
Else "-1"
Added a new formula that isn't used on the report called Numbers
If {#N}="1"
Then 1
Else -1
And then I changed the sum formula to match the intermediate field
Sum({#Numbers})
I still don't know the reason why the report didn't want to show me the numbers in the N formula, nor why the sum formula didn't calculate the correct sum.
I hope this is at least going to be useful to someone else,
Cheers!

How do you print nicely in LEDA

How do you print 'nicely' in LEDA?
I am looking for the equivalent to
fixed
setprecision(3)
I have got a little further with this.
There is a difference between precision and output precision.
I can set LEDA's output precision with e.g bigfloat::set_output_precision(4);
but I still do not know what LEDA's equivalent to fixed is.
Also, I do not know what the latest LEDA of todouble is
I have tried mu.todouble() where mu is declared as a bigfloat, but I get a compiler error
A further answer is mu.to_double()

Resources