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
Absurd Alhazred
Mar 27, 2010

by Athanatos

Evil_Greven posted:

I hate this:
C++ code:
void blah(uint32_t x) {
  int y = static_cast<int>(x); //32-bit unsigned to 64-bit signed is fine right?
  if(0 < y) {
    //do something with y
  }
}
0 was the default value, no negative was expected since value was from unsigned, and most significant bit was set in x.
C++ is the horror here.

I feel kind of lost. For the purpose the comment is talking about, this should be int64_t instead of int, just to be sure, right? But then for this purpose, just do if (0 < x), because for unsigned that's equivalent to if (0 != x)? Even if something 64-bitish is happening with y later, you could do the conversion inside the if segment instead of before the conditional.

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You need a really unusual target for int to be 64 bits, because it would mean you didn’t have a standard integer type for at least one of 8/16/32 bits. That’s the sort of poo poo you see on some obscure Cray from the 90’s, probably with a note like “Cray changed this for the next series because of customer complaints that it was too difficult to program.”

If int were 64 bits, the code would work; promoting an unsigned type to a wider signed type is value-preserving.

Evil_Greven
Feb 20, 2007

Whadda I got to,
whadda I got to do
to wake ya up?

To shake ya up,
to break the structure up!?
int64_t would have been a safer option. They were using the int for some bit shifting and such, and that's not so safe:
C++ code:
#include <iostream>
#include <stdint.h>
int main() {
  uint32_t x = 2147483648;
  std::cout << "x:  "<< x << std::endl;
  int y = static_cast<int>(x);
  std::cout << "y: " << y << std::endl;
  int64_t z = static_cast<int64_t>(x);
  std::cout << "z: " << z << std::endl;
  return 0;
}
Yields (e: no int isn't really 64-bit):
code:
x: 2147483648
y: -2147483648
z: 2147483648

Evil_Greven fucked around with this message at 05:18 on Aug 6, 2022

Xarn
Jun 26, 2015
AFAIK a recently (2019) retired supercomputer also used 64 bit wide plain ints.

But in the common case, expecting plain int to be 64 bits is one of those "do you even C++ bro?" things.

cheetah7071
Oct 20, 2010

honk honk
College Slice
I only use plain int when the width isn't even something I'm thinking about. If I make a conscious decision that 32 bits is the correct size, I'll call it int32_t

Xarn
Jun 26, 2015
Anyway, the real horror is Go.

We have a C++ library with Go bindings. One of the things you can ask for is to serialize some internal data structures with precomputed data, so that you can reuse the computations across runs, thus saving time.

We naively implemented this using C.GoBytes helper, which you feed a ptr+size pair and it gives you a byte slice that's copy of the data. This worked, until recently, when we got a bug report from external team about the serialization panicking.

Turns out, while we properly use size_t type for the size info in C, GoBytes uses C.int. Which is 32 bits (see last few posts), and is signed, thus overflows. :ughh:

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

Evil_Greven posted:

I hate this:
C++ code:
void blah(uint32_t x) {
  int y = static_cast<int>(x); //32-bit unsigned to 64-bit signed is fine right?
  if(0 < y) {
    //do something with y
  }
}
0 was the default value, no negative was expected since value was from unsigned, and most significant bit was set in x.
C++ is the horror here.

How do I know that's not 32 bit unsigned to 32 bit signed?

ephphatha
Dec 18, 2009




Because it's 32 bit unsigned to a signed type of at least 16 bits :pseudo:

SurgicalOntologist
Jun 17, 2004

I'm not a JS/TS developer, but surely there's a better way.

JavaScript code:
  private getFileName(index: string): string {
    if (index.length === 1) {
      return `0000${index}-video.mp4`;
    }
    if (index.length === 2) {
      return `000${index}-video.mp4`;
    }
    if (index.length === 3) {
      return `00${index}-video.mp4`;
    }
    if (index.length === 4) {
      return `0${index}-video.mp4`;
    }
    if (index.length === 5) {
      return `${index}-video.mp4`;
    }
  }

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
I think there's an NPM package for that.

SurgicalOntologist
Jun 17, 2004

There's no built-in way to do string formatting?

I should add, index is originally an int, and the caller line is
JavaScript code:
    const recordName = this.getFileName(index.toString());

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
It's a joke about NPM.

Deffon
Mar 28, 2010

The non-joke answer is padStart.

Also, you can keep "index" as a "number" instead of a "string", it's automatically converted to a string when printed or interpolated.

nielsm
Jun 1, 2009



Deffon posted:

The non-joke answer is padStart.

But that doesn't work in Internet Explorer!

OddObserver
Apr 3, 2009

nielsm posted:

But that doesn't work in Internet Explorer!

Luckily the article has a pollyfill! (It's actually slightly more complicated than it seems since it supports multi-char padding and might need to truncate that).

Dross
Sep 26, 2006

Every night he puts his hot dogs in the trees so the pigeons can't get them.

Deffon posted:

Also, you can keep "index" as a "number" instead of a "string", it's automatically converted to a string when printed or interpolated.

But then you don’t know how many characters it is post-conversion. Although I suppose you can padStart based on the length of the entire string.

Deffon
Mar 28, 2010

Dross posted:

But then you don’t know how many characters it is post-conversion. Although I suppose you can padStart based on the length of the entire string.

You don't need the length of the string, the resulting zero-padded number should always be 5 digits according to the code.
Forgot that index was supposed to be the thing to call padStart on so an explicit conversion is needed somewhere at least either by the caller or the function.

TypeScript code:
const padded = index.toString().padStart(5, '0')
return `${padded}-video.mp4`
Gives "00123-video.mp4" for 123.

NihilCredo
Jun 6, 2011

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

Deffon posted:

The non-joke answer is padStart.

That's a really unusual name for the "add N characters to the left of this string" function. I wonder why they couldn't choose a more standard one?

Dross
Sep 26, 2006

Every night he puts his hot dogs in the trees so the pigeons can't get them.

Deffon posted:

You don't need the length of the string, the resulting zero-padded number should always be 5 digits according to the code.
Forgot that index was supposed to be the thing to call padStart on so an explicit conversion is needed somewhere at least either by the caller or the function.

TypeScript code:
const padded = index.toString().padStart(5, '0')
return `${padded}-video.mp4`
Gives "00123-video.mp4" for 123.

Yeah, if I were writing it I would do that transformation separately and explicitly even though I don’t use typescript, but you mentioned just coercing it to a string implicitly by interpolating it which is what I was responding to

QuarkJets
Sep 8, 2008
Probation
Can't post for 3 hours!

NihilCredo posted:

That's a really unusual name for the "add N characters to the left of this string" function. I wonder why they couldn't choose a more standard one?

"pad" is standard vernacular for "append data to the leading and/or trailing edge of some other data". Other terms like "fill" are used sometimes but "pad" is probably the one I most commonly see

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

QuarkJets posted:

"pad" is standard vernacular for "append data to the leading and/or trailing edge of some other data". Other terms like "fill" are used sometimes but "pad" is probably the one I most commonly see

(it was a left-pad joke)

Vanadium
Jan 8, 2005

I guess someone showed up wearing a Unicode hat and informed people that not all scripts go left-to-right.

Doom Mathematic
Sep 2, 2008
Naming stuff in JavaScript is sometimes hard because there are popular third-party libraries which add functionality which the language designers themselves then later decide to add.

Xarn
Jun 26, 2015
If only you could design language in a way where you can tell where the symbol comes from. You could then group names into spaces based on their origin.

Soricidus
Oct 21, 2010
freedom-hating statist shill
That’s not quite enough, because people want to add methods that extend existing data types. Like, extending methods. Extendy methods. Something like that.

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

Soricidus posted:

That’s not quite enough, because people want to add methods that extend existing data types. Like, extending methods. Extendy methods. Something like that.

Just use a function pointer, sounds like what you're after.

Beef
Jul 26, 2004
Xarn was making a joke about namespaces existing. Or, the extra layers of joke indirections in this thread have overflowed my mental stack.

CPColin
Sep 9, 2003

Big ol' smile.
Sounds like you should store your jokes on the heap, like I store all my posts :smug:

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
a heap of dung

CPColin
Sep 9, 2003

Big ol' smile.
And a stack of bodies :mad:

Carbon dioxide
Oct 9, 2012

https://twitter.com/t3dotgg/status/1557987235642822656

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Someone jokingly suggested yoink and yeet for Swift's upcoming ownership modifiers, so there's a chance!

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

pokeyman posted:

Someone jokingly suggested yoink and yeet for Swift's upcoming ownership modifiers, so there's a chance!
And Rust isn't far behind!

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

pokeyman posted:

Someone jokingly suggested yoink and yeet for Swift's upcoming ownership modifiers, so there's a chance!

I would genuinely be pleased if someone aliased those to new and delete in a codebase I had to touch.

Bruce Hussein Daddy
Dec 26, 2005

I testify that there is none worthy of worship except God and I testify that Muhammad is the Messenger of God
code:
tempBool = CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt32.Checked + opt36.Checked) + opt40.Checked) + opt60.Checked) + opt24.Checked) + opt28.Checked) + opt30.Checked) + opt34.Checked) + opt35.Checked

If tempBool = True Then
tempBool = CShort(CShort(CShort(CShort(optlessthan152.Checked + optbetween152and203.Checked) + optmorethan203.Checked) + optlessthan155.Checked) + optbetween155and205.Checked) + optmorethan205.Checked
If tempBool = True Then
tempBool = CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt26.Checked + opt25.Checked) + opt24.Checked) + opt23.Checked) + opt22.Checked) + opt21.Checked) + opt20.Checked) + opt19.Checked) + opt18.Checked) + Option17.Checked) + Option18.Checked) + Option15.Checked) + Option14.Checked) + Option13.Checked) + Option12.Checked
End If
End If
Variable names changed slightly to make it a little harder to tell what kind of application this in, that's all. The entire project is like this.

Macichne Leainig
Jul 26, 2012

by VG
Fun fact, VB.NET is about old enough to drink in the United States

NihilCredo
Jun 6, 2011

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

Bruce Hussein Daddy posted:

code:
tempBool = CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt32.Checked + opt36.Checked) + opt40.Checked) + opt60.Checked) + opt24.Checked) + opt28.Checked) + opt30.Checked) + opt34.Checked) + opt35.Checked

If tempBool = True Then
tempBool = CShort(CShort(CShort(CShort(optlessthan152.Checked + optbetween152and203.Checked) + optmorethan203.Checked) + optlessthan155.Checked) + optbetween155and205.Checked) + optmorethan205.Checked
If tempBool = True Then
tempBool = CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt26.Checked + opt25.Checked) + opt24.Checked) + opt23.Checked) + opt22.Checked) + opt21.Checked) + opt20.Checked) + opt19.Checked) + opt18.Checked) + Option17.Checked) + Option18.Checked) + Option15.Checked) + Option14.Checked) + Option13.Checked) + Option12.Checked
End If
End If
Variable names changed slightly to make it a little harder to tell what kind of application this in, that's all. The entire project is like this.

In VB.NET Booleans convert to -1 for True and 0 for False, and any non-zero value converts to True as a boolean, so basically this is a horror way of writing

code:
tempbool = (opt1 or opt2 or ...) and (opt4 or... ) and (opt5 or ...)
or in other words, "at least one checkbox in each of these three groups must be checked"

Beef
Jul 26, 2004

Bruce Hussein Daddy posted:

code:
tempBool = CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt32.Checked + opt36.Checked) + opt40.Checked) + opt60.Checked) + opt24.Checked) + opt28.Checked) + opt30.Checked) + opt34.Checked) + opt35.Checked

If tempBool = True Then
tempBool = CShort(CShort(CShort(CShort(optlessthan152.Checked + optbetween152and203.Checked) + optmorethan203.Checked) + optlessthan155.Checked) + optbetween155and205.Checked) + optmorethan205.Checked
If tempBool = True Then
tempBool = CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt26.Checked + opt25.Checked) + opt24.Checked) + opt23.Checked) + opt22.Checked) + opt21.Checked) + opt20.Checked) + opt19.Checked) + opt18.Checked) + Option17.Checked) + Option18.Checked) + Option15.Checked) + Option14.Checked) + Option13.Checked) + Option12.Checked
End If
End If
Variable names changed slightly to make it a little harder to tell what kind of application this in, that's all. The entire project is like this.

I'll make the safe bet and say it's life sciences.

Foxfire_
Nov 8, 2010

code:
detonate_the_nuclear_missiles = CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(CShort(opt26.Checked + opt25.Checked) + opt24.Checked) + opt23.Checked) + opt22.Checked) + opt21.Checked) + opt20.Checked) + opt19.Checked) + opt18.Checked) + Option17.Checked) + Option18.Checked) + Option15.Checked) + Option14.Checked) + Option13.Checked) + Option12.Checked

Adbot
ADBOT LOVES YOU

champagne posting
Apr 5, 2006

YOU ARE A BRAIN
IN A BUNKER

Beef posted:

I'll make the safe bet and say it's life sciences.

oooh I would've gone boomer electrical engineer

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