Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Presto
Nov 22, 2002

Keep calm and Harry on.
Hey, at least whoever wrote the number-to-words function did a good job though. It correctly handled a number in the trillions.

Adbot
ADBOT LOVES YOU

Foxfire_
Nov 8, 2010

I'm from a country that uses whichever trillion scale you don't use and say the number-to-words function is garbage! :argh:

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

TypeScript code:
function checkValue(input: string): number | boolean {
  if (someRegexp.test(input)) {
    return NaN;
  }
  return true;
}

Votlook
Aug 20, 2005

Bonfire Lit posted:

TypeScript code:
function checkValue(input: string): number | boolean {
  if (someRegexp.test(input)) {
    return NaN;
  }
  return true;
}

This malice, not stupidity,

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:

Bonfire Lit posted:

TypeScript code:
function checkValue(input: string): number | boolean {
  if (someRegexp.test(input)) {
    return NaN;
  }
  return true;
}

I dont know TypeScript what the gently caress is happening here

Tei
Feb 19, 2011

D34THROW posted:

I dont know TypeScript what the gently caress is happening here

I don't know either.

Seems to be some sort of validation function that can return NaN if a regex checks, otherwise return true.

I guess the reason why is posted here is having a function that can return only two values true and NaN, and not using a boolean for that.

It would made more sense to me to return null instead of NaN, or simply false.

null being defined in database realms has "I dunno, man".

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
It's just about the worst named function I've ever seen. What it's supposed to do depends heavily on someRegExp which I'm assuming is a constant. If it was something that checked whether something is a number I suppose it could be something to test whether a string can be parsed to a number, although who the hell knows why you'd return NaN if it doesn't.

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man

enki42 posted:

It's just about the worst named function I've ever seen. What it's supposed to do depends heavily on someRegExp which I'm assuming is a constant. If it was something that checked whether something is a number I suppose it could be something to test whether a string can be parsed to a number, although who the hell knows why you'd return NaN if it doesn't.

Because then it's not a number op couldn't be simpler

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!
I don't know how you write the signature for that return value in Typescript and not realize that you're doing something very, very wrong.

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?
It’s the sort of thing I’d write if I were quitting tomorrow and hated my coworkers.

Jazerus
May 24, 2011


D34THROW posted:

I dont know TypeScript what the gently caress is happening here

it's bad enough that the language hands you NaNs occasionally, writing a function that does it intentionally is a war crime

ExcessBLarg!
Sep 1, 2001

Bonfire Lit posted:

TypeScript code:
function checkValue(input: string): number | boolean {
  if (someRegexp.test(input)) {
    return NaN;
  }
  return true;
}
I'm not familiar with TypeScript either so the only way this makes sense to me is if these assumptions are true and it's being used in this context:

1. NaN coerces to false in boolean context.
2. This function is being called as a short circuit OR operation.
3. The input is a string representation in of a rational (fraction) that needs to be converted (evaled) into a floating-point value.
4. The purpose is to catch an integer divide by zero before evaling the string, to avoid a divide by zero exception and instead provide a reasonable (if imprecise) floating-point response.

Mata
Dec 23, 2003
I imagine if you are writing that sort of function you're probably also playing fast and loose with JS == operator wherever that returned NaN shows up

Jazerus
May 24, 2011


ExcessBLarg! posted:

I'm not familiar with TypeScript either so the only way this makes sense to me is if these assumptions are true and it's being used in this context:

1. NaN coerces to false in boolean context.
2. This function is being called as a short circuit OR operation.
3. The input is a string representation in of a rational (fraction) that needs to be converted (evaled) into a floating-point value.
4. The purpose is to catch an integer divide by zero before evaling the string, to avoid a divide by zero exception and instead provide a reasonable (if imprecise) floating-point response.

NaN does coerce to false, but JS/TS don't freak out when you divide by zero, they return infinity, a special number that is sort of but not exactly like mathematical infinity, which is a reasonable if imprecise response to dividing by zero. feeding NaN into something that expects a number, however, will cause an error.

e: which, idk, maybe you'd rather get an error than send infinity out into the world in this case, hard to say

Jazerus fucked around with this message at 18:23 on Feb 21, 2022

Ola
Jul 19, 2004

I'm not familiar with Typescript either, but I'm pretty sure the type signature means it's a union type so it returns either a number or a boolean. It's fine in that regard, apart from being horrible in all regards. I'm guessing it's written to check if it's a number and the caller does a check on != NaN, for some strange reason that probably involves lots of broth and many cooks.

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?

Ola posted:

…check if it's a number and the caller does a check on != NaN, …

Hahaha I bet it does exactly this lol

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Today I learned NaN is not a valid literal type in TypeScript, further confirming the excellent taste of its maintainers.

canis minor
May 4, 2011

NaN is in fact a Number

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Jazerus posted:

it's bad enough that the language hands you NaNs occasionally, writing a function that does it intentionally is a war crime

You'll be glad to hear then that the return value of that function is never used except in boolean context (i.e., if (checkValue(foo)) { do something })

Ola
Jul 19, 2004

Bonfire Lit posted:

You'll be glad to hear then that the return value of that function is never used except in boolean context (i.e., if (checkValue(foo)) { do something })

So kind of what I guess, only the other way around. Why, for the love of all that is holy, not if (someRegexp.test(input)) { do something } ?

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Beats me! I've elected not to check source control because I suspect if I do I'll just get salty at whoever wrote that and whoever reviewed it afterwards.

Jazerus
May 24, 2011


Bonfire Lit posted:

You'll be glad to hear then that the return value of that function is never used except in boolean context

for now

Foxfire_
Nov 8, 2010

ExcessBLarg! posted:

4. The purpose is to catch an integer divide by zero before evaling the string, to avoid a divide by zero exception and instead provide a reasonable (if imprecise) floating-point response.
Surprise! Javascript/typescript doesn't have integers

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?

Bonfire Lit posted:

Beats me! I've elected not to check source control because I suspect if I do I'll just get salty at whoever wrote that and whoever reviewed it afterwards.

So this person is also in the habit of eval()-ing strings, you say?

BigPaddy
Jun 30, 2008

That night we performed the rite and opened the gate.
Halfway through, I went to fix us both a coke float.
By the time I got back, he'd gone insane.
Plus, he'd left the gate open and there was evil everywhere.


Always fun when you solve a problem on AWS by making a copy of the function that is not working, set it up the same, the code is the same, the permissions are the same and it works when the previous one you copied it from and should be identical throws errors with no message.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Foxfire_ posted:

Surprise! Javascript/typescript doesn't have integers

Says you.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Bonfire Lit posted:

Beats me! I've elected not to check source control because I suspect if I do I'll just get salty at whoever wrote that and whoever reviewed it afterwards.

You can just say it was you

Tei
Feb 19, 2011

I did this on a javascript console.

NaN?true:false;

it returns false, so NaN evaluates to false, so thats why that function works. while NaN is not a boolean, evaluated in a boolean context it works like a false.


this is C spirit working from inside javascript, convenient if tricky

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Tei posted:

this is C spirit working from inside javascript, convenient if tricky
more like a spirit like Jack Daniels is

Ola
Jul 19, 2004

Tei posted:

I did this on a javascript console.

NaN?true:false;

it returns false, so NaN evaluates to false, so thats why that function works. while NaN is not a boolean, evaluated in a boolean context it works like a false.


this is C spirit working from inside javascript, convenient if tricky

It isn't in a boolean context though, it's in a union type context where it can be either a number or a boolean. Union types are cool and good in functional languages, but Typescript isn't one of those.

NaN is extra super falsy in Javascript so you really wouldn't want to use it for anything.

code:
false==false
true
NaN==NaN
false
NaN===NaN
false

Xarn
Jun 26, 2015
That's just NaN behaviour in ieee 754.

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
How the hell is a NaN not strictly equal to NaN :psyduck:

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

D34THROW posted:

How the hell is a NaN not strictly equal to NaN :psyduck:

How could it be? It holds a meaning outside its bit set.

Tei
Feb 19, 2011

Ola posted:

code:
false==false
true
NaN==NaN
false
NaN===NaN
false

:-O

what the gently caress. I mean... null in databases is probably similar, but what the gently caress

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Tei posted:

:-O

what the gently caress. I mean... null in databases is probably similar, but what the gently caress

This is standard behavior for floats. Anything else would be against the spec and extremely wrong. What were you expecting?

ToxicFrog
Apr 26, 2008


D34THROW posted:

How the hell is a NaN not strictly equal to NaN :psyduck:

First time, eh?

ynohtna
Feb 16, 2007

backwoods compatible
Illegal Hen

Tei posted:

:-O

what the gently caress. I mean... null in databases is probably similar, but what the gently caress

It's NaN-defined behaviour.

nielsm
Jun 1, 2009



NaN by definition represents an unknowable value, so you have to assume that two NaN values are different.
Usually SQL NULL represents the absence of a value, while NaN represents that a value is present but unknowable.

Tei
Feb 19, 2011

leper khan posted:

This is standard behavior for floats. Anything else would be against the spec and extremely wrong. What were you expecting?

https://www.youtube.com/watch?v=6ZpaWOLjWx0

For it to explode comically, like in a old movie, when computers where still called "electronic brains".

Adbot
ADBOT LOVES YOU

OddObserver
Apr 3, 2009
Signaling NaNs are indeed also a thing, but not in JS.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply