[Math] and Variables

by GuyThomas at 2013-01-11 07:53:04

Can you give me an explaination of why I get different result when I assign a [Math]::static value to a variable, compared with if I use the raw expression?

$Ans1 = [Math]::Round(33.9890090034)
Write-Host "Round 1 "$Ans1
Write-Host "Round 2 " [Math]::Round(33.9890090034)

---------------------------------
Results:
Round 1 34
Round 2 [Math]::Round 33.9890090034
by DonJ at 2013-01-11 08:06:13
$Ans1 is defaulting to a type of [int]. If you want it to contain more precision, assign the value to [float]$Ans1. I think that returns a single-precision.
by GuyThomas at 2013-01-11 08:50:12
My point is that $Ans1 does not exactly equal [Math]::Round(33.9890090034). $Ans1 is, in my terms, a pure number, whereas when I combine [Math]::Round(33.9890090034) with Write-Host PowerShell treats it as text.
by ArtB0514 at 2013-01-11 09:04:39
Try it this way [Write-Host really would like you to send it a single "proper" string to output]. Use $() to enclose an expression you would like to have evaluated as part of the string.:

$Ans1 = [Math]::Round(33.9890090034)
Write-Host "Round 1 $Ans1"
Write-Host "Round 2 $([Math]]
by nohandle at 2013-01-11 09:11:04
long story short, the write-host takes the math part as string because you did not instructed it to do otherwise, use parentheses and you get what you want.
Write-Host "Round 2 " ([Math]]
by DonJ at 2013-01-11 09:12:29
Yeah, sorry - bad explanation. I gotta quit reading these on my phone.

In your first example, $Ans1 contains exactly what it should - a rounded 33.989(etc), which is 34. In your second example, as Art points out, PowerShell is displaying your math formula as a literal string.

Write-Host, by the way, is not a terribly awesome cmdlet. You’re also technically doing string concatenation wrong. Anytime you want to include a variable or an expression inside a string, do as Art suggested and include them directly within the string, using double quotes, either as a straight variable or as a subexpression. Also try and avoid Write-Host unless your sole purpose is to display a string of text, as pixels, on the screen and only the screen.
by GuyThomas at 2013-01-11 09:18:48
Two wonderful and fulsome explanations, thank you. I get it now!
by nohandle at 2013-01-11 09:49:42
[quote="DonJ"]Anytime you want to include a variable or an expression inside a string, do as Art suggested and include them directly within the string, using double quotes, either as a straight variable or as a subexpression.[/quote] Sorry but I can’t fully agree on this. The rule is great when you already have some experience. You have proper code, so you need to expand just variables or simple expressions you are sure how to write correctly.
But being a beginner you just cut yourself from the code highlighting. And believe me this leads to hundreds of re-runs trying to figure out how to write the three commands long awesome subexpression combo you just invented and want to include in the output. Been there. :slight_smile:
by DonJ at 2013-01-11 10:09:29
Whether you cut yourself off from code highlighting depends on the editor you’re using. The v3 ISE highlights variables and subexpressions inside quotation marks, so you’re not giving up anything. Believe me, I’ve seen people struggle with this as well - I teach a few thousand students a year - and that’s why I encourage the use of a good editor that can correctly color syntax, even in quoted subexpressions.

You’re also bringing a separate thread into the subject. Just because something is a good or best practice does not necessarily mean it is the easiest to interpret for a beginner. They’re separate issues. I usually start beginners out with a very structured approach, but deliberately try to lead them toward better practices throughout the class. If you don’t show folks the right way to do something, they’ll never learn. In other words, you’re talking about what you think is easier to teach and read, which is a valid point.

That said, massively concatenated strings are not, in my experience, easier for beginners to figure out.


"The server " + $server.name + " status is " + $status


Always causes me more grief in the classroom than

"The server $($server.name) status is $status"


Once I’ve explained what the latter is doing. Obviously, your experience may differ. I make a point of building the instructional narrative to lead to the latter, and I’m working with students who’ve (obviously) come to a class and aren’t trying to pick stuff up on their own. Students with a lot of prior VBScript experience obviously gravitate to concatenating because it’s something they’re used to. Once they see subexpressions, they grasp them quite quickly and are quite grateful for them.
by nohandle at 2013-01-11 14:06:10
Don: Thanks for sharing your experience. :slight_smile: