Show forward slash in haml? - haml

In haml, I want to display 5 / 174. numerator, foward slash, denominator.
= #numerator
/
= denominator
My workaround:
= #numerator
- slash = "/"
= slash
= denominator

I would just use interpolation with #{} for something like this:
#{#numerator} / #{denominator}
(note you don’t need = at the start).
If you really want to start a line in Haml with the / character (or any other special character) you can escape it with \ e.g.
= #numerator
\/
= denominator

Related

Parse from String and convert to float, integer (Raku)

FAQ: In Raku, how do I parse a String and get a Number ? For example:
xxx("42"); # 42 (Int)
xxx("0x42"); # 66 (Int)
xxx("42.123456789123456789"); # 42.123456789123456789 (Rat)
xxx("42.4e2"); # 4240 (Rat)
xxx("42.4e-2"); # 0.424 (Rat)
Just use the prefix +:
say +"42"; # 42 (Int)
say +"0x42"; # 66 (Int)
say +"42.123456789123456789"; # 42.123456789123456789 (Rat)
say +"42.4e2"; # 4240 (Rat)
say +"42.4e-2"; # 0.424 (Rat)
Info
val a Str routine is doing exactely what you (I) want.
Beware that it is returning Allomorph object. Use unival or just + prefix to convert it to Number
Links:
Learning Raku: Number, Strings, and NumberString Allomorphs
Same question in Python, Perl
Roseta Code: Determine if a string is numeric
Edited thanks to #Holli comment
my regex number {
\S+ #grab chars
<?{ defined +"$/" }> #assertion that coerces via '+' to Real
}
#strip factor [leading] e.g. 9/5 * Kelvin
if ( $defn-str ~~ s/( <number>? ) \s* \*? \s* ( .* )/$1/ ) {
my $factor = $0;
#...
}

Square root Iteration using x1 = (x0 - a / x0) / 2

Here is what I have so far:
Public static double Sqrt (double a){
double xOld = a / 2;
double xNew = 0;
while (Math.abs(xOld - xNew) >= 0.0001 {
xNew = (xOld + a / xOld) / 2;
xNew = xOld;
}
}
I need to use the algorithm x1 = (x0 - a / x0) / 2 to find the approximate square root of a number. a is the original number and x0 starts at the value a / 2. When I run this code I get 12.5 (which is a / 2). What i need help on is what xNew value to initialize and the last line to the while loop. Thanks for any help
TRY THIS ON FOR SIZE:
Public static double Sqrt(double a) throws IllegalArgumentException {
if (a < 0.0) throw new IllegalArgumentException();
double aSqrt = a / 2.0;
while (Math.abs(a - aSqrt*aSqrt) >= 0.0001) { // I'd use a smaller tolerance
double aSqrtPrev = aSqrt;
aSqrt = (aSqrtPrev + a / aSqrtPrev) / 2.0;
}
return aSqrt ;
}
Probably what you want is xOld = xNew at the end of your loop, as the way it is set up now, you immediately overwrite the value of xNew that you just calculated.
But the above is not enough. With xNew and xOld equal at the end of the loop, you guarantee that the while loop will exit on the next pass, given the test condition. Try using a different condition for the while loop, such as Math.abs(a - xNew*xNew) >= 0.0001 or similar.
Another way to do it would be to keep your test condition as-is, but then you would have to swap the order of the two statements in the while loop, still do the assignment xOld = xNew.

How to use a variable in the format specifier statement?

I can use:
write (*, FMT = "(/, X, 17('-'), /, 2X, A, /, X, 17('-'))") "My Program Name"
to display the following lines on the console window:
-----------------
My Program Name
-----------------
Now, I want to show a pre-defined character instead of - in the above format. I tried this code with no success:
character, parameter :: Chr = Achar(6)
write (*, FMT = "(/, X, 17(<Chr>), /, 2X, A, /, X, 17(<Chr>))") "My Program Name"
Obviously, there are another ways to display what I am trying to show by means of a variable in the format specifier statement. For instance:
character, parameter :: Chr = Achar(6)
integer :: i, iMax = 17
write (*, FMT = "(/, X, <iMax>A1, /, 2X, A, /, X, <iMax>A1)") (Chr, i = 1, iMax), &
"My Program Name", &
(Chr, i = 1, iMax)
However, I would like to know if there is any way to use a variable or invoke a function in the format specifier statement.
The code you are trying to use (<>) is not standard Fortran. It is an extension accepted by some compilers. Just build the format string as a string.
"(/, X, 17(" // Chr // "), /, 2X, A, /, X, 17(" // Chr // "))"
For the the numeric case you have to prepare a string with the value
write(chMax, *) iMax
"(/, X, " // chMax // "A1, /, 2X, A, /, X, " // chMax // "A1)"
or you can use some function, if you have it
"(/, X, " // itoa(iMax) // "A1, /, 2X, A, /, X, " // itoa(iMax) // "A1)"
but it may still be preferable to call it beforehand, to avoid multiple calls.
The function can look like:
function itoa(i) result(res)
character(:),allocatable :: res
integer,intent(in) :: i
character(range(i)+2) :: tmp
write(tmp,'(i0)') i
res = trim(tmp)
end function

Variable Width String Padding

I am being passed a string value that could begin with 0-4 decimal digits and ends with a small, but unknown, amount of non-decimal text. I need to pad the leading decimal digits out to four digits with the "0" character. If I am passed "abcd", I would return "0000abcd". If I passed "0xyz", I would return "0000xyz". And so on...
I am using the following code to detect how much padding I need to do:
int padding = 0;
int strlen = [rawString length];
NSRange rng = [rawString rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
if (rng.length > 0 && rng.location < 4) // Characters were found at the end.
padding = 4 - rng.location;
else if (rng.length == 0 && strlen < 4) // Only numbers were found.
padding = 4 - strlen;
Now that I can accurately determine the padding my challenge is to add the padding in an efficient manner. I could do something like the following, but it just feels inefficient to me.
for (; padding > 0; padding--)
rawString = [#"0" stringByAppendingString:rawString];
Is there a better way to add a variable amount of padding to a string like the one I described?
EDIT: I considered chopping off the numerical portion, padding it, and then adding it back, but the number of corner cases seemed to indicate that an in situ solution would be best.
Since padding is between 0 and 4, you could do this:
rawString = [[#"0000" substringFromIndex:4-padding] stringByAppendingString:rawString];
The idea is to chop off the right number of zeros from a string containing four elements.

Rounding an Objective-C float to the nearest .05

I want to round the following floating point numbers to the nearest 0.05.
449.263824 --> 449.25
390.928070 --> 390.90
390.878082 --> 390.85
How can I accomplish that?
The match the output in your question, you can do the following:
float customRounding(float value) {
const float roundingValue = 0.05;
int mulitpler = floor(value / roundingValue);
return mulitpler * roundingValue;
}
Example:
NSLog(#"Output: %f --> %.2f", 449.263824, customRounding(449.263824));
There's the round() function. I think you need to do this:
double rounded = round(number * 20.0) / 20.0;
As with all floating point operations, since 1/5 is not directly representable as a binary value, you'll see bizarre not quite exact results. If you don't like that, you can use NSDecimalNumber's -decimalNumberByRoundingAccordingToBehaviour: method but it'll be a bit slower.
I know the question is answered but I used the following code:
float unrounded = 2.234;
float decimal = 0.05;
float decimal2 = 1/decimal;
float rounded = (((int)((unrounded*decimal2)+0.5))/decimal2);
For example:
> unrounded = 2.234
> decimal = 0.05
> decimal2 = 1/0.05 = 20
>
> rounded:
> 2.234 * 20 = 44.68
> 44.68 + 0.5 = 45.18
> make an integer: 45
> 45 / 20 = 2.25
You could use an NSNumberFormatter to carry out rounding and indeed to specify the rounding you require via one of the NSNumberFormatterRoundingMode options. (Search for "NSNumberFormatterRoundingMode" in the above class reference to see the defaults.)
However, as #Jesse states in the comment on your question, there doesn't seems to be any standard form of rounding going on in the examples you're provided.
If it were round to the nearest x, then you could go with:
roundedValue = originalValue + x * 0.5;
roundedValue -= fmodf(roundedValue, x);
As it is, it isn't entirely clear what you want.
Use floor:
#include <math.h>
...
double result = floor(number * 20.0) / 20.0;