libera/#maemo/ Thursday, 2018-08-23

brolin_empeyKotCzarny: Yes, with “1.0/3” or “1/3.0” the result is correct.  I forgot that integer division is different than decimal/float division.04:54
DocScrutinizer05hail strict typing09:59
DocScrutinizer05this implicit typing is a mess09:59
DocScrutinizer05there shouldn't even be a thing like "integer division", only cast while assigning results to variables10:01
KotCzarnysay that to fpu less cpus10:02
DocScrutinizer05and it's the implicit hidden cast that makes me throw up. A decent compiler would throw an error or at least a bold fat warning when assigning a (possibly) non-integer result to an integer variable10:03
DocScrutinizer05int i = int( a / b )10:04
DocScrutinizer05int i = a / b   BZZZZZZZ JACKASS!10:04
bencohactually the current behavior is both sane and accurate10:05
DocScrutinizer05it is, but not obvious10:05
DocScrutinizer05as the description implies: it's implicit10:05
bencohyou just should not expect a float result when dividing integers10:05
KotCzarnythat's a compiler's nuance, or language?10:06
DocScrutinizer05you always should expect a float result when dividing10:06
bencohit might be implicit, but nothing indicates it should not be the case10:06
bencohabsolutely not10:06
KotCzarnyif language, it's ok, if compiler, it's black magic10:06
KotCzarny(for new users)10:06
DocScrutinizer05it's langiage, for c10:06
bencohKotCzarny: C standard10:06
KotCzarnythen it's up to the user to learn properly10:07
bencohmost probably K&R or c89, at least10:07
DocScrutinizer05it's prolly B already ;-D10:07
bencohhaha, could be10:07
DocScrutinizer05aka: the most eloquent assembler ever made10:07
MaxdamantusJust to be clear: the issue is implicit conversion, not implicit typing.10:12
bencoh(or the lack thereof)10:13
bencoh(which is a good thing in my opinion)10:13
MaxdamantusThe issue is its precense.10:13
Maxdamantuspresence*10:13
bencoh?10:13
DocScrutinizer05of course it's a matter of personal preferences10:13
DocScrutinizer05I lack half of the convo, sorry10:14
Maxdamantusimo 5/4 == 2 is acceptable, 5/4.0 == 2.5 should be a type error.10:14
bencohMaxdamantus: ah10:14
bencoh(5/2.0 I guess)10:14
Maxdamantuser, yeah10:14
KotCzarnyimo current behavior of c is ok10:15
bencohKotCzarny: I think so as well, but ... :)10:15
MaxdamantusI would probably also lean on the side of having separate operators for floating and integral division.10:15
DocScrutinizer05how do you add a ".0" to int i; ?10:15
bencohDocScrutinizer05: you cannot; you have to cast it10:15
KotCzarnysimilarly to whole pointer thingy, tracking and remembering variable types and expected return types is up to the dev10:15
DocScrutinizer05:-P10:15
KotCzarnyin c++ otoh yeah, compiler should nag the poor user to death with warnings/errors10:16
KotCzarnybencoh, i think i+=.0 will work ;)10:16
MaxdamantusC has a bunch of other issues around implicit conversions, eg: int x = 4; unsigned y = -4; x < y;10:17
KotCzarnyunless you want to change type10:17
DocScrutinizer05in my book a operation (divide) that could yield a float WILL yield a float, always. If you want "integer divide" you got operators and cast for that10:17
Maxdamantuseh, should've been the other way round10:17
KotCzarnymaxd: in that case you get a nice, juicy warning10:17
Maxdamantusint x = -4; unsigned y = 4; y < x;10:17
bencohKotCzarny: i+=0. won't change anything to i10:18
bencohyou cannot change the type of a variable10:18
DocScrutinizer05ORLY? :-D10:18
bencohDocScrutinizer05: I dunno why, but he seemed to imply that it would10:19
DocScrutinizer05hehehehe10:19
bencoh(or was that sarcasm? :])10:19
KotCzarnybencoh, question was not clear (i've went with how to add .0 in value to i ;)10:19
bencohhuhu10:19
Maxdamantus20:17:33 < KotCzarny> maxd: in that case you get a nice, juicy warning10:20
MaxdamantusI don't seem to get any warning with `gcc -Wall -pedantic`10:20
KotCzarnyhmm, let me check10:20
MaxdamantusThat's using 4.9.3 though, a bit old.10:20
MaxdamantusStill no warning with 6.4.010:21
bencohwell, it's valid behavior, although I would expect a compiler warning as well10:21
bencohthey do warn about unused variables, so ...10:21
bencohnot warning about that is silly10:22
KotCzarnyoh right. it's in -Wextra10:23
KotCzarny(in 4.9.2 at least)10:23
MaxdamantusHeh, don't think I've ever used -Wextra10:23
KotCzarnyit's nice to catch some more obvious brainfarts ;)10:23
Maxdamantusmore obvious, or less obvious? Presumably less.10:24
MaxdamantusAnyway, in a sensible language, that's just a type error.10:24
KotCzarnymore as in numerical number of them10:24
Maxdamantusbecause you should have something like `(>) :: a -> a -> boolean`, where `a` is any given number type.10:25
KotCzarnyc assumes dev knows what he/she is doing10:25
MaxdamantusSo `(>) :: unsigned -> signed -> boolean` is not valid.10:25
KotCzarnyand if you know that int x wont get into negatives or y more than half of the type, it will work10:26
KotCzarnymany bugs prove it wrong though ;)10:26
KotCzarnybut that's C, love it, learn it or go into some sandbox10:26
KotCzarnyit's like trying to apply political correctness to the programming language10:27
MaxdamantusThose are exclusive, right?10:28
bencohMaxdamantus: both learn and love C, you mean?10:28
Maxdamantusbencoh: yes.10:28
bencohI somehow fell in love with it :)10:28
MaxdamantusI mostly just got amused by figuring out weird things based on reading the standards over and over again.10:29
Maxdamantuseg, like how you can pass arrays to functions in C90 but not C99.10:29
Maxdamantusbut in either case, you can't do anything with those passed arrays.10:29
bencohtheir are some standard oddities, yeah10:29
bencohthere*10:29
Maxdamantusthough gnu89 lets you use the array in those cases where you can pass it (but that's non-standard)10:30
KotCzarnyisnt string an array?10:30
KotCzarny;)10:30
MaxdamantusNot exactly.10:31
Maxdamantusin case anyone's interested with the array passing thing: struct a { int a[10]; }; struct a foo(void){ struct a r = {0}; return r; } void bar(int a, ...){} int main(void){ bar(42, foo().a); }10:35
MaxdamantusIn C90, that passes an array to `bar`. In C99 and C11 it passes a pointer that is invalid once control actually reaches the function.10:36
Maxdamantusand in gnu89, I think you can actually write `int a[10] = va_arg(ap, int [10]);` to get it again.10:38
Maxdamantusin C89 itself, you can at least write `va_arg(ap, a10);` given `typedef int a10[10];` (but not necessarily `va_arg(ap, int [10])`, since `va_arg` is only specified as taking an identifier as the type), but you can't do anything with it except pass it to another variadic since it's a non-lvalue of array type.10:41
Maxdamantusanother variadic function*10:42
bencohMaxdamantus: what do you mean by "array" in that case?10:45
Maxdamantusbencoh: in which case?10:45
bencohis the whole array stored in the stack?10:45
bencohc9010:45
Maxdamantusbencoh: if you think about it as a stack and of all arguments being on the stack, yes.10:45
bencohthat sounds odd tbh, but ... :)10:46
MaxdamantusUsually you don't get to handle arrays as values.10:46
bencoh(I mean, such a definition sounds odd)10:46
Maxdamantusbecause array expressions end up just being pointers in all sensible contexts other than when they appear as the operand of &.10:47
Maxdamantusbut `foo().a` is a weird case, since `foo().a` is not an lvalue, so the array → pointer rule doesn't apply to it in C90.10:47
bencohyeah10:47
bencohah10:47
bencohI see10:47
MaxdamantusC99 added the extra case of non-lvalue array expressions though, so in C99 it does turn into a pointer, which means you can then access one of its members.10:48
Maxdamantus(in C90, `foo().a[0]` is invalid when `foo().a` is an array)10:48
bencohsounds like a standard "bug" :)10:48
KotCzarnyis there any gain of passing it as array instead of just pointer?10:48
bencoh(bug-by-design I mean)10:48
MaxdamantusKotCzarny: it would be logically sensible to be able to pass array values around.10:49
bencohKotCzarny: well, the pointer is eventually invalid, while copied data would remain valid10:49
MaxdamantusKotCzarny: C doesn't let you do that (except in this weird case), so when that's useful you pretty much just have to put the array inside a struct. struct values can be passed around.10:49
KotCzarnyso it might even be slower because it has to copy the function argument?10:50
MaxdamantusKotCzarny: possibly. It depends how you're using it and what sort of optimisations the compiler is able to make.10:51
MaxdamantusKotCzarny: you should compare it to something like structs .. sometimes it's useful to pass a struct value, sometimes it's useful to pass a pointer to a struct object.10:52
MaxdamantusLogically, you should be given the same choice with arrays. It's probably just for historical reasons that you're not given that choice.10:53
Maxdamantusin other modern languages that provide a C-style distinction between pointers/references and non-pointer/reference product types, they offer that choice properly.10:54
Maxdamantus(particularly, Go and Rust)10:54
Maxdamantusin Go, an `int[3]` value itself is equivalent to three ints, instead of being a pointer to an object containing 3 ints.10:55
MaxdamantusSo when you pass an `int[3]`, you're passing a copy of those three ints, just as when you pass an `int` or a `struct { a int }`, you're passing a copy of that int.10:56
MaxdamantusYou can make a slice (basically a pointer) out of it using something like `a[:]`, which is pretty much what C does implicitly whenever you write an array expression (other than when it's the operand of &)10:57
bencohactually, passing objects (others than standard int/float and pointers) as arguments always sounded odd to me11:00
Maxdamantusobjects are not things you pass, unless you mean reference/pointer.11:01
Maxdamantusin languages like Java, "object" and "reference" are often conflated.11:01
bencohI mean passing a struct, for instance11:02
MaxdamantusAh.11:02
bencohit always looks wrong to me, although I know you can11:02
KotCzarnyi always think of struct as an array with multiple types inside11:03
MaxdamantusWell, if you have something like a 2D vector, you'd normally want to pass a single value containing both components.11:03
MaxdamantusPassing a pointer in that case would be a bit like passing a pointer every time you wanted to pass a single number.11:03
KotCzarnybut for a compiler it's just memory area11:03
Maxdamantusalso, it's often a lot cleaner being able to return structs than having to take in a "return pointer".11:04
Maxdamantus`struct player player = new_player(4, 5);` rather than `struct player player; new_player(&player, 4, 5);`11:05
Maxdamantusimo the former is nicer .. some might disagree.11:05
Maxdamantusand obviously with the 2D vector example .. `point x = plus(dot_prod(a, b), dot_prod(c, d))` rather than `point x, ab, cd; dot_prod(&ab, &a, &b); dot_prod(&cd, &c, &d); plus(&x, &ab, &cd);`11:09
KotCzarnylatter has a bonus of not needed for additional variable11:09
KotCzarny*no need11:10
MaxdamantusIndeed.11:10
Maxdamantus(former, rather)11:10
KotCzarnythe one that takes &result as a func param11:10
MaxdamantusIt's the other one that has fewer variables.11:11
MaxdamantusThe latter has to declare variables to store the "results" of `dot_prod`11:11
KotCzarnyi meant that compiler doesnt have to copy the result11:11
Maxdamantus(`ab`, `cd`)11:11
KotCzarnyand just operate on result itself11:11
MaxdamantusIf either is faster, I'd expect it to be the former.11:12
KotCzarnyyou can't overload operators in c, so it's not that useful there (but has its uses)11:13
Maxdamantussince it would probably end up just splitting the struct values across different registers.11:13
Maxdamantusso `dot_prod(a, b)` would essentially be compiled down to `dot_prod(a.x, a.y, b.x, b.y)`11:13
KotCzarnyaren't you passing pointer?11:13
KotCzarnywell, mem address to a struct11:14
MaxdamantusNo. `point` is meant to be a plain struct in both cases.11:14
Maxdamantustypedef struct { double x, y; } point;11:14
KotCzarnytbh i dont know if you use only one field of the struct in function to cause compiler to unpack it wholly11:14
KotCzarnyi would assume it just acts as if that was a pointer11:15
MaxdamantusThere shouldn't really be anything to "unpack".11:16
Maxdamantusaside from potential padding, wrapping things inside a struct in C should normally be a zero-cost abstraction.11:16
Maxdamantus`struct { int a; }` should be passed around pretty much the same way that an `int` is passed around.11:16
Maxdamantusin memory, they should look the same.11:16
MaxdamantusThat's contrary to an object in Java .. if you write `class Wrapped { int a; }`, that's not a zero-cost abstraction.11:17
Maxdamantussince any `Wrapped` value is a pointer.11:17
MaxdamantusThey're specifically working on fixing that issue by creating a new concept called "value classes"11:18
Maxdamantushttp://openjdk.java.net/jeps/16911:18
bencoh"working" on that kind of stuff, in 2018, sounds kinda funny, for a 20yo language :)11:19
MaxdamantusWell, most languages in the last 20 years haven't provided such a thing.11:19
Maxdamantusin most languages all you've had is objects.11:19
Maxdamantusand maybe a finite number of non-reference primitive types.11:20
Maxdamantus(because of that, "primitive" is often taken to mean "non-reference" nowadays)11:20
MaxdamantusI suspect Go and Rust are pretty much the only relatively known-about languages that allow you to specify your own non-reference types.11:21
Maxdamantus(made in the last 20 or 30 years)11:21
bencohC structs can be non-reference11:23
MaxdamantusYes, and C was made almost 50 years ago11:24
MaxdamantusI'm saying that practically every well-known language made in the last 20 or 30 years has been unlike C in this regard.11:24
bencohah, I see your point :)11:24
Maxdamantusbut they're now trying to fix it, since the concept already exists in Go and Rust, and they're trying to introduce it finally to Java.11:25
KotCzarnysince i'm fixing up sysarm product i would prefer to wait until one is available (if you can arrange similar value, ie. ~55months of 4TB in /ca)11:29
KotCzarnywhoops11:30
KotCzarnywrong chan11:30

Generated by irclog2html.py 2.17.0 by Marius Gedminas - find it at https://mg.pov.lt/irclog2html/!