# An exercise in mathiness

•  Here is a Java exercise from a certain Java textbook (I don't actually know the title or author, since all I got was this photocopy). Try to figure out how many things are wrong with it (I count at least three).

• The real question is, where the fuck did those numbers come from?  At first I thought that they might have done something stupid like printed the sine, cosine, and tangent and labeled then arcsin, arccos, and arctan.  Then I saw that arccos is 1.02 so that can't be the cosine of anything.   Sigh, textbooks.

•  The sad part is that I actually figured out how they got those numbers. I was hoping someone would figure it out.

Here goes:

1) convert the user's input into radians

2) use that radian value as an INPUT to the arc* functions

•  Proving once again that those that can't do, teach.

• @shill said:

The sad part is that I actually figured out how they got those numbers. I was hoping someone would figure it out.

Here goes:

1) convert the user's input into radians

2) use that radian value as an INPUT to the arc* functions

Wait, how does bashing your head result in the numbers? I understood 1) and 2)...

This is why math functions should always have 'units' attributes. Certain functions are only valid on unitless parameters, like inverse trig functions, logarithms, etc. This shouldn't compile at all and give an error like "ERROR: Cannot perform inverse trigonometry on parameters with units." "ERROR, cannot add value with units 'a' to value with units 'b' ." "ERROR: Cannot assign value with units 'q' to value with units 'r' ."

Actually, it would be nice if the universe could throw errors like "FATAL ERROR: Continued attempts to perform <insert activity here> without sufficient ability" more often.  (So far, the universe is only good at throwing those errors with high-powered applicances and/or sharp objects.)

This is why math functions should always have 'units' attributes. Certain functions are only valid on unitless parameters, like inverse trig functions, logarithms, etc.
Except you'll get fucked by units that aren't really units, like degrees, radians, decibels, etc.

• @Welbog said:

you'll get fucked by units that aren't really units, like degrees, radians, decibels, etc.
How are degrees not really units?

Certain functions are only valid on unitless parameters, like inverse trig functions, logarithms, etc.

No.

log(5 meters)-log (2 meters) = log(5/2).

•

Dang forum kept refusing to accept my update!

@Welbog said:

This is
why math functions should always have 'units' attributes. Certain
functions are only valid on unitless parameters, like inverse trig
functions, logarithms, etc.
Except you'll get f****d by units
that aren't really units, like degrees, radians, decibels, etc.

Eh,
radians is just a name, it's not a unit.  Well, technically 'radians'
is the same thing as the 'unitless' unit (whee!).  Degrees is
definitely a unit though.  The distinction: you can raise a number to a
power in radians, but you can't raise a number to a power in degrees.
Oddly enough, this is why you can't use units in the inverse trig
functions, because those are just exponentials.

Decibels...well,
thinking about if that's a unit or not will take more brainpower than I
want to expend right now on my lunch break.

@aka said:

No.

log(5 meters)-log (2 meters) = log(5/2).

Commence the banging of heads on tables. HINT:  that is not a valid equation.

Edit: Another hint: log(5) - log(2) = log(5 meters / 2 meters) is valid.

•  @Welbog said:

This is why math functions should always have 'units' attributes. Certain functions are only valid on unitless parameters, like inverse trig functions, logarithms, etc.
Except you'll get fucked by units that aren't really units, like degrees, radians, decibels, etc.

I don't know what's meant by "unit attributes". Hopefully he's talking about creating named types such as Degree, Radian, Decibel.

<font face="courier new,courier">double tan(Degree);</font>

• @savar said:

I don't know what's meant by "unit attributes". Hopefully he's talking about creating named types such as Degree, Radian, Decibel.

<font face="courier new,courier">double tan(Degree);</font>

Nope, not talking about named types because you'd have to overload every math operator for every combination of units. What I'm talking about is an attribute associated with numeric values. Consider something like this (C++ style syntax - and I can't remember throws syntax at the moment as I've been stuck in embedded-C land and am too lazy to look it up - but it should be clear what I mean anyway):

```class ValueWithUnits{
double val;
String unit;
ValueWithUnits& operator=(ValueWithUnits&) throws UnitsMismatchError;
ValueWithUnits& operator+(ValueWithUnits&) throws UnitsMismatchError;
ValueWithUnits& operator*(ValueWithUnits&); //and so on
ValueWithUnits() { val = 0; unit = ""; }
};
ValueWithUnits& operator=(ValueWithUnits& rvalue)
{
if(unit != rvalue.unit) // units must be equal!
throw new UnitsMismatchError;
else
val = rvalue.val;
return *this;
}
ValueWithUnits& operator*(ValueWithUnits& term)
{
units += "-" + term.units;  // multiplcation combines units
value *= term.value;
return *this;
}```

@aka said:

No.

log(5 meters)-log (2 meters) = log(5/2).

Commence the banging of heads on tables. HINT:  that is not a valid equation.

Edit: Another hint: log(5) - log(2) = log(5 meters / 2 meters) is valid.

What on earth are you talking about? It's perfectly well-defined.

• Holy crap the edit timer is short:

Yes, I do realize that the
code I wrote for operator* is really the code for operator*=.... but
that's largely irrelevant for the point I was making.

By
the way, aka, if you can tell me what exactly e^(1 meter) equals, then
I'll accept that log(5 meters)-log(2 meters) = log(5/2) might be valid.

HINT:
e^(1 meter) equals neither 2.71828... nor 2.71828... meters.

Holy crap the edit timer is short:

Yes, I do realize that the
code I wrote for operator* is really the code for operator*=.... but
that's largely irrelevant for the point I was making.

By
the way, aka, if you can tell me what exactly e^(1 meter) equals, then
I'll accept that log(5 meters)-log(2 meters) = log(5/2) might be valid.

HINT:
e^(1 meter) equals neither 2.71828... nor 2.71828... meters.

Surprisingly, e^(1 meter) equals 1/e^(-1 meter).  But your question is beside the point, as a definition

of exp(unit) isn't required in the above equation at all, not even one of exp(log(unit)), one merely requires

that exp(-log(unit))*exp(log(unit))=1. Which is, of course, the whole point. Let me rephrase that:

HINT: this is, of course, the whole point.

And that's why people of substantial erudition routinely use log(...) - log(...) in numerical code and

get away with it.

• @aka said:

Surprisingly, e^(1 meter) equals 1/e^(-1 meter). But your question is beside the point, as a definition

of exp(unit) isn't required in the above equation at all, not even one of exp(log(unit)), one merely requires

that exp(-log(unit))*exp(log(unit))=1. Which is, of course, the whole point.

Which is, of course, total and utter nonsense (except that indeed a definition of exp(unit) isn't required but a definition of log(unit)).

@aka said:

Let me rephrase that:

HINT: this is, of course, the whole point.

And that's why people of substantial erudition routinely use log(...) - log(...) in numerical code and

get away with it.

Mathematical functions have a strong tendency to take dimensionless arguments, and I have yet to meet or read a paper by a person of even mediocre, let alone substantial, erudition who would even write something like
"log(5 metres) - log(2 metres)" except as a quotation. The thought that somebody would routinely do that is terrifying.

• @aka said:

Surprisingly, e^(1 meter) equals 1/e^(-1 meter).

...

And that's why people of substantial erudition routinely use log(...) - log(...) in numerical code and

get away with it.

I will grant that log(a unit_a)-log(b unit_b) = log(a/b) works as a special case when unit_a and unit_b are identical; it does not work for arbitrary units.

So I suppose this means that we're both right and both wrong as neither of us fully specified the constraints on our statements.

That lack of specification is the error that causes things to fail the most often in my observation.

• How are degrees not units?

With degrees, radians, and decibels, either the standard units involved cancel out, or were never involved in the first place.  Radians, for example, are derived units, whereas kilograms are not.   Calculation of decibel values involves ratios of logs in which the units cancel out.  A degree is 1/360 of a circle's diameter--but no particular circle, because there's no such thing as a 'standard circle,' where there IS such a thing as a 'standard kilogram' or 'standard meter.'

• @Ilya Ehrenburg said:

@aka said:
Surprisingly, e^(1 meter) equals 1/e^(-1 meter). But your question is beside the point, as a definition

of exp(unit) isn't required in the above equation at all, not even one of exp(log(unit)), one merely requires

that exp(-log(unit))*exp(log(unit))=1. Which is, of course, the whole point.

Which is, of course, total and utter nonsense (except that indeed a definition of exp(unit) isn't required but a definition of log(unit)).

@aka said:

Let me rephrase that:

HINT: this is, of course, the whole point.

And that's why people of substantial erudition routinely use log(...) - log(...) in numerical code and

get away with it.

Mathematical functions have a strong tendency to take dimensionless arguments, and I have yet to meet or read a paper by a person of even mediocre, let alone substantial, erudition who would even write something like
"log(5 metres) - log(2 metres)" except as a quotation. The thought that somebody would routinely do that is terrifying.

Oh. So, what is integral[x1..x2] dx/x?

• @aka said:

Oh. So, what is integral[x1..x2] dx/x?

Provided
x1 and x2 are positive real numbers, it's log(x2) - log(x1), which of
course equals log(x2/x1). With a few caveats this also holds for
nonzero complex numbers. But what's that to do with the price of beer?
There are no units or dimensions involved.

•  @Ilya Ehrenburg said:

@aka said:

Oh. So, what is integral[x1..x2] dx/x?

Provided
x1 and x2 are positive real numbers, it's log(x2) - log(x1), which of
course equals log(x2/x1). With a few caveats this also holds for
nonzero complex numbers. But what's that to do with the price of beer?
There are no units or dimensions involved.

Noooooo...how would they. Never, since the inception of the world, has there been a need to do an integral over

a dimensionful quantity. -- Why don't you google for, say, '"log T" + Fermi' and be surprised about the amount of

mediocrity...

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.