Ruby | 2 float number multiple not giving the exact value [duplicate] - ruby

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I have 2 float number and I try to get the multiple. But in the console it does not get the correct one. I do not know maybe there is a leak ?
{"price"=>"0.049391", "size"=>"0.001"}
When I multiple them console shows;
************
0.049391
************
0.001
************
4.9390999999999995e-05
************
Then when I push this data to array and in front end when I console.log
I get;
0.000049390999999999995
Why it is not 0.0000493901 ?

4.9390999999999995e-05 and 0.000049390999999999995 are the same number. e-05 is nothing more than notation for "move the decimal place five spots to the left".

Related

Expanding IP ranges into single IP addresses using Linux bash command? [duplicate]

This question already has answers here:
bash - for loop for IP range excluding certain IPs
(2 answers)
IP range generator script [closed]
(1 answer)
Closed 5 years ago.
Which is the best method for me to handle this? I'm thinking awk might be the way to go perhaps, but not sure where to start even.
I have a text file, which contains IP addresses and ranges as per this example:
10.10.115.69
10.10.128.6 - 10.10.128.7
10.10.128.20
10.10.128.28
10.10.128.38 - 10.10.128.53
10.10.128.70 - 10.10.128.71
10.10.128.130 - 10.10.128.144
10.10.128.232 - 10.10.128.233
10.10.130.5
10.10.132.5
I'm trying to get them all into their own individual address. So as per above on the line '10.10.128.38 - 10.10.128.53' .. I would want that to convert to:
Expected output:
10.10.128.20
10.10.128.28
10.10.128.38
10.10.128.39
10.10.128.40
<snip>
10.10.128.52
10.10.128.53
10.10.128.70
10.10.128.71
<etc.>
Keeping of course the single host IPs included into output, just expanding the range parts.
Hope that makes sense.
Edit: Comments are suggesting this is a duplicate of another exact question. Please could someone link to it for me, as I'm not finding it. I am new here so not quite up to speed with things.
My expected output is as above, I'm not after a generator, rather a way to expand the ranges, the lines with '-' in them <start IP> - <end_IP> while keeping the single ones that are there still.

swift for-in loop in playground just counts loops, does not print

Just getting started with Swift, looking at the Wikipedia article, it has this code:
// Define a dictionary with four items:
// Each item has a person's name and age
let people = ["Anna": 67, "Beto": 8, "Jack": 33, "Sam": 25]
// Now we use Swift's flexible enumerator system
// to extract both values in a single loop
for (name, age) in people {
print("\(name) is \(age) years old.")
}
Putting this into a playground in XCode, I expected this to see this:
Anna is 67 years old.
Beto is 8 years old.
etc.
Instead, in the right-hand panel, it just shows this:
(4 times)
There is a button over on the far right, beyond the (4 times) which, when I click it, causes Jack is 33 years old. to display within the loop. (See screen shot below.) Click the button again, and that disappears. Can anyone help me understand the logic behind all this?
Small issue in your code you should include a parameter "\(name)" not (name) in order to print it. so you should use like this
for (name, age) in people {
print("\(name) is \(age) years old.")
}
If you want to show all the value as you asked on question just follow these steps :-
**Step 1**
Clicks the + sign right corner.Then it shows latest value.
Step 2
If you want to show all the values clicks the recent value and right click and choose value history
Step 3
Then you can see the all values.
what you are missing is called String interpolation. So you for printing a value of a variable/constant by concatinating it with string you have to do it like this.
print("\(name) is \(age) years old.")
Secondly you have to press (⇧⌘Y) in order to see the output in debuging area

Xpath/xQuery difference in months

I am trying to get the difference between two dates in months for an xPath.
I have no problems getting it in days (1127)
days-from-duration(xs:date('2012-06-17')-xs:date('2009-05-17'))
When I try doing it in months I get 0
months-from-duration(xs:date('2012-06-17')-xs:date('2009-05-17'))
I did notice that this comes back as days only ("P1126D") so that maybe the problem just not sure how to fix it.
xs:date('2012-06-17')-xs:date('2009-05-17')
Thanks for any help!
So the best thing I can seem to do is manually calculate it.
(year-from-date(xs:date('2012-06-17')) - year-from-date(xs:date('2012-05-18')))*12 + month-from-date(xs:date('2012-06-17')) -month-from-date(xs:date('2012-05-18')) + (if (day-from-date(xs:date('2012-06-17')) < day-from-date(xs:date('2012-05-18')) ) then -1 else 0)
This function was not included in the spec because we couldn't agree semantics for it. We heard arguments that the difference between 31 March 2015 and 30 Sept 2015 was six months, and arguments that it was five months. (Advice: negative differences seem to be even more problematic than positive differences.) You can easily find that a date plus 6 months minus 6 months is not the date where you started. You'll have to define what you think the answer should be, and implement it using lower-level facilities such as month-from-date and year-from-date.
The problem is that days-from-duration returns the days field -- not the number of days the duration includes. Likewise for months and years.
Now, you can divide by days, but not by months (since months have uneven boundaries):
(: arg1 here is a dayTimeDuration, which can't be easily converted to a yearMonthDuration :)
let $arg1 := xs:date('2012-06-17')-xs:date('2009-05-17')
return xs:dayTimeDuration($arg1) div xs:dayTimeDuration("P30D")

??? Error: File: generation1.m Line: 43 Column: 1 Function definitions are not permitted at the prompt or in scripts [duplicate]

This question already has answers here:
In MATLAB, can I have a script and a function definition in the same file?
(7 answers)
Closed 9 years ago.
1.Bring the image to frequency domain by applying the DCT
2.generate a watermark signal
3.Use the thousand largest coefficients of the original image to embed a watermark sequence of length 1000.
4.Coefficients are modified according to the stream bits of the message using the equation below,
|CAW = CA (1 + α Wi)|
5.Extraction process – simply subtracting the original DCT coefficients from the watermarked image coefficients
function WM_plot(r,c,ext_wm,orig_wm)
for k=1:1000
wm=randn(r,c);%depending on the size of the watermark
wm=uint8(wm);%if necessary
store(k)=WM_detect(ext_wm,wm);%wrong watermarks
if k == 400
store(k)=WM_detect(ext_wm,orig_wm);%original watermark detection
end
end
figure(1),plot(1:k,[store]),ylabel('Watermark detector response'),xlabel('random watermarks');
hold on
%threshold calculation
[peak,ind]=sort(store,'descend');
threshold=peak(2)+(peak(2)*0.1);%T=second highest peak+10percentof the same
figure(1),plot(1:1000,[threshold],'red');
hold on
figure(1),plot(1:1000,peak(2),'green');
The error «Function definitions are not permitted at the prompt or in scripts» — tells you that you trying to define function in a wrong place. According to the similar question at matlab answers, you should :
Write each function in an editor file and save it as the title of function, e.g:
function hello_world(hObject,evt)
fprintf(2,'Hello World!');
end
saved in hello_world.m
and after that you should call your functions from your main application.

R script line numbers at error? [duplicate]

This question already has answers here:
How to get R script line numbers at error?
(6 answers)
Closed 6 years ago.
I found this post from a year ago, and I'm using R version 2.11.1 (2010-05-31), but still getting error messages without line numbers.
Any solution?
The answers given there are still valid. Returning line numbers from a script ain't that straight-forward, but R can give you a lot more information on where the error can be found.
You could use the error options to save the info in a file, for example :
options(error = quote({
sink(file="error.txt");
dump.frames();
print(attr(last.dump,"error.message"));
traceback();
sink();
q()}))
The function findLineNum() could be used if you have the name of the file somewhere available. If you have the error message, you could do something like :
dump.frames()
x <- attr(last.dump,"error.message")
ll <- gsub("Error in (.*) : .*","\\1",x)
lln <- findLineNum(srcfile,ll)
In the upcoming R 2.14, the core team is making progress toward implementing this feature. Functions in scripts loaded with source(file=..., keep.file=TRUE) will contain an attribute srcref, which identifies the range of characters corresponding to the function's definition in an in-memory copy of the source file stored as an object of class srcfilecopy.
This does not immediately provide line-level debugging, but it lets you get approximate line numbers if you're willing to get your hands dirty. Also, it's progress.

Resources