Bash - how to sort negative values? [duplicate] - bash

This question already has answers here:
Linux sort doesn't work with negative float numbers
(3 answers)
Closed 6 years ago.
is there a way to sort negative numbers with sort in bash ? I have sin written out
...
0.250109
0.188852
0.126850
0.064349
0.001593
-0.061168
-0.123689
-0.185722
-0.247023
-0.307349
...
and the problem is when I run sort on it, it just sorts it by values - regardless of the minus in front of some values. Is there a way to fix it ? Thanks

Use sort -g (--general-numeric-sort), not sort -n (--numeric-sort).
See sort Invocation for an explanation of the subtle differences between these two options.

my problem was that the data wasnt well formatted - because of my locale, I had to sed decimal point into decimal comma - that's how its written in czech republic
thanks

Related

Why is the Ruby Math.log function returning the wrong values? [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
Given 3^5 (3**5 or 3*3*3*3*3) = 243.
Why/how does Ruby do the following:
n = 243
Math.log(n,3)
returns:
4.999999999999999
Math.log(243)/Math.log(3)
returns:
4.999999999999999
3**Math.log(n,3)
returns:
242.99999999999977
That last one really gets me. Something is going wrong here? I'm missing something? Both?
Thanks!
Values are not wrong, this is because floating point precision is limited.

Arithmetic with variable [duplicate]

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 8 years ago.
I need to perform some arithemic with bash.It goes like this
VariableA = (VariableB-VariableC) / 60
Variable A should be approximated to 2 decimal places
I don't know which one of these is the right answer(Don't have a linux server at hand atm to test)
VariableA = $((VariableB-VariableC)/60)
VariableA = $(((VariableB-VariableC)/))/60)
It would be nice if someone could also help me out about how to round the VariableA to 2 decimal places without using third party tools like bc
The bash itself can compute only integer values, so if you need to use a fixed number of decimals, you can shift your decimal point (it's like computing in cents instead of dollars or euros). Then only at the output you need to make sure there's a . before the last two digits of your number:
a=800
b=300
result=$((a*100/b)) # factor 100 because of division!
echo "${result:0:-2}.${result: -2}"
will print 2.66.
If you want to make computations in floating points, you should use a tool like bc to do that for you:
bc <<<'scale=2; 8.00/3.00'
will print out 2.66.

Print integer with "most appropriate" kilo/mega/etc multiplier [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert byte size into human readable format in java?
Given an integer, I'd like to print it in a human-readable way using kilo, mega, giga etc. multipliers. How do I pick the "best" multiplier?
Here are some examples
1 print as 1
12345 print as 12.3k
987654321 print as 988M
Ideally the number of digits printed should be configurable, e.g. in the last example, 3 digits would lead to 988M, 2 digits would lead to 1.0G, 1 digit would lead to 1G, and 4 digits would lead to 987.7M.
Example: Apple uses an algorithm of this kind, I think, when OSX tells me how many more bytes have to be copied.
This will be for Java, but I'm more interested in the algorithm than the language.
As a starting point, you could use the Math.log() function to get the "magnitude" of your value, and then use some form of associative container for the suffix (k, M, G, etc).
var magnitude = Math.log(value) / Math.log(10);
Hope this helps somehow

Is there a way to use sort_by on a second criterion if the first is equal? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Sort strings and numbers in Ruby
I have an array of place objects, each with a name (unique) and number (non-unique). Is there a simple way to use sort_by to first sort by number and then (within each number) by name?
I know I can write a custom block for sort, but if this is possible, it'd be even easier!
Not sure if this is what you mean by "custom block" but it seems pretty simple to me:
places.sort_by { |place| [place.number, place.name] }

Compare two versions of a text file and find additions/removals with Ruby? [duplicate]

This question already has answers here:
diff a ruby string or array
(12 answers)
Closed 8 years ago.
I am tracking changes in a web-page using Ruby. After I removed all html tags and blank lines, I get an array of lines which needs to be checked for additions/removals assuming that there may be repetitions. Could you recommend a good gem if it has been done already?
I could make the array lines unique and then the problem is avoided. But what if I need to track the repeated lines as well with respect to their position in the text?
Sounds like a textbook case of where you'd want to use the Diff algorithm.
There's a 'diff' gem, although to be fair I've never used it: http://rubygems.org/gems/diff

Resources