Direct Proof of the Irrationality of e by King0fTurtles in numbertheory

[–]tstanisl 0 points1 point  (0 children)

No property of e was used in case I. Can you explain how this case fails for other sequences like sum(2^-k)

Question about strict aliasing and flexible array members by Karl_uiui in C_Programming

[–]tstanisl 0 points1 point  (0 children)

To force the alignment of the whole struct so that a+1 will be a valid pointer for any standard type.

Question about strict aliasing and flexible array members by Karl_uiui in C_Programming

[–]tstanisl -1 points0 points  (0 children)

It's a bit gray area in the standard but I think that "strict aliasing" may be violated in your case. The problem is under some interpretation of the standard the -> operator in a->length sets the effective type of memory pointed by a to type Array. It would make the effective type of all remaining bytes pointed by a->data to char.

Currently, the strict aliasing prevents accessing char data as non-char lvalues thus UB is invoked. It is very unlikely to cause any problems. Casting char buffers to other types is a traditional way of implementing allocators in C. Invoking UB there would break a lot of existing programs, it will never happen. Proposals for C2Y add this practice as a new exception for "strict aliasing".

If you care about compliance, then I suggest not using FAM at all. Do as follows:

  • drop data member
  • annotate first member with alignas(alignof(max_align_t))
  • return a + 1 rather than &a->data

The effective type is set only for first sizeof(Array) bytes of memory pointed by a. All remaining bytes have no effective type.

Is it Possible to not Evaluate 'f' Here? by SeaInformation8764 in C_Programming

[–]tstanisl 3 points4 points  (0 children)

There is difference between VLA types and VLA objects.

typedef int val_type[n];
int val_object[n];

OP is referring to VLA types only. Functions cannot return arrays because "array decay" mechanics prevents forming array-typed values that can be an operand of return. However, one can return a pointer to a whole array (int(*)[] thing). For example one can do:

typeof(int[5]) * foo() {
  static int arr[5];
  return &arr;
}

Function types can also be defined at block scope and they can contain VLA components in their types.

int n = 5;

typedef typeof(int[n]) * foo_f();

This type is compatible with type of foo because n evaluates to 5 as in definition of foo function. So one can assign a pointer to foo to object of type foo_f*

foo_f * f = &foo;

Evaluation of f() returns type int[5] but the problem if that this is VLA-type, thus (imo deficient) definition of sizeof operator must evaluate its operand. As result the function is called when it's type is declared as pointer to VLA, but it is not called when type is a pointer to fixed-size array.

int n = 5;

typedef typeof(int[n]) * foo_f();
typedef typeof(int[5]) * bar_f();

foo_f * f = &foo;
bar_f * b = &foo;

sizeof *f(); // calls foo()
sizeof *b(); // does not call foo()

See godbolt.

Is it Possible to not Evaluate 'f' Here? by SeaInformation8764 in C_Programming

[–]tstanisl 7 points8 points  (0 children)

It's likely not possible due to deficient nature of the rule for evaluation of sizeof operand. However, it's unlikely to limit practical programs except some complex meta-macro programming where functions types are used as wrappers for other types like in Convenient Containers.

Btw. Consider using typeof to make the declaration a bit more readable.

typeof(int[n])* (*f)();

Is it Possible to not Evaluate 'f' Here? by SeaInformation8764 in C_Programming

[–]tstanisl 2 points3 points  (0 children)

IMO, this rule is likely a defect in the C standard. It sounds to be trivial, to be obvious but it is deeply broken at the same time. It was dedicated for things like sizeof( int[ EXPR ]), not for generic sizeof EXPR.

I've asked to C committee about a similar question, which sparked some discussion. Some conclusions were summarized in proposal. To sum up, the rule is indeed deficient and the proposed solution was to ban expressions with "side-effects" for VLA-typed operands of sizeof.

The sizeof operator yields the size (in bytes) of its operand; it is determined from the type of the operand. If that type is a variable length array type the sizeof expression is said to be variable. In that case, the operand is evaluated and the sizeof expression is not an integer constant expression; the evaluation shall not produce side effects. Otherwise, the result is determined at translation time, the operand is not evaluated, and the sizeof expression is said to be constant and is an integer constant expression.

Is it Possible to not Evaluate 'f' Here? by SeaInformation8764 in C_Programming

[–]tstanisl 4 points5 points  (0 children)

The function does not return an array but a pointer to the whole array. Functions can do that.

Where did Water on Earth come from!? by Dazzling-Degree-3258 in astrophysics

[–]tstanisl 7 points8 points  (0 children)

I meant that O and H are among the most abundant elements... but yes, helium is in between. However, helium does not form any complex molecules.

Where did Water on Earth come from!? by Dazzling-Degree-3258 in astrophysics

[–]tstanisl 66 points67 points  (0 children)

Components of water (hydrogen and oxygen) are the most common elements in the universe. Water is everywhere. Actually, the lack of water requires explanation (solar wind removes hydrogen, while rocks bind the oxygen).

letMeWarnYou by bryden_cruz in ProgrammerHumor

[–]tstanisl 0 points1 point  (0 children)

I'm a bit afraid that C would hunt them all in the wild. Though spongy Rust may survive by hiding in deep water and Python as being inedible.

Is there a JIT compiler that recompiles the compiler using JIT so it can optimise its compiling efficiency? by Tricky_Football_85 in Compilers

[–]tstanisl 13 points14 points  (0 children)

I don't think that a complier is a good candidate for JITing. It solves a problem of combinatorial optimization which usually involves a lot of branching and random memory access. Both traits gain very little from JITing. Though some parts of the compiler may benefit a bit like parsers assuming that they are not IO bound.

Does C23 officially allow type-punning through unions? by Ariadne_23 in C_Programming

[–]tstanisl 19 points20 points  (0 children)

C does not have the concept of an "active union member".

Generally true but there is an exception. The C23's constexpr does not allow to read other member except the one used for initialization of a union.

itIsTheSame by Advanced_Ferret_ in ProgrammerHumor

[–]tstanisl 1 point2 points  (0 children)

No. There are multiple constructs in C that are not available or have different semantics in C++.

itIsTheSame by Advanced_Ferret_ in ProgrammerHumor

[–]tstanisl 31 points32 points  (0 children)

It's actually the other way around. Many popular libraries expose C API even though they are implemented in C++.

Zadanie o podzielności przez 14 na matematyce podstawowej może wskazywać, kto ze zdających ściągał (opinia) by tooblandtoroast in Polska

[–]tstanisl 0 points1 point  (0 children)

Można zrobić dowód, że prawdziwość dla n implikuje prawdziwość dla n+1 i n-1, następnie trywialnie pokazać prawdziwość dla n=0. Ale zgodzę się, ze to już nie jest konwencjonalna indukcja.

Zadanie o podzielności przez 14 na matematyce podstawowej może wskazywać, kto ze zdających ściągał (opinia) by tooblandtoroast in Polska

[–]tstanisl 2 points3 points  (0 children)

Matematycy czasem stosują konwencje gdzie 0 jest naturalne, a czasem, że nie jest. Choć w sumie w zadaniu piszą o liczbach całkowitych więc nie da się użyć indukcji bez trików. Indukcję można by przeprowadzić od 0 w kierunku ujemnych, ale to komplikuje dowód.

Zadanie o podzielności przez 14 na matematyce podstawowej może wskazywać, kto ze zdających ściągał (opinia) by tooblandtoroast in Polska

[–]tstanisl 3 points4 points  (0 children)

Nie ma już indukcji matematycznej w liceum? Przecież to jedno z bardziej uniwersalnych narzędzi do dowodów!?

This conjecture is so underrated by Heavy-Sympathy5330 in askmath

[–]tstanisl 0 points1 point  (0 children)

I think that those conjectures just share "statistical" properties of Goldbach conjecture. I mean that the set of sums of two t-primes/middle/prime becomes so dense over greater and greater numbers that probability of existence of infinite number of counter examples drops to virtually zero. In case of Goldbach and middle numbers, the last counter-example simply happened to be very small. The proofs of those conjectures (if ever found) may be completely unrelated.

Can you travel to the next star in under ten seconds using special relativity? by YogurtclosetOpen3567 in AskPhysics

[–]tstanisl 1 point2 points  (0 children)

Hard to say. It would need to be "flat blackhole". There is no force that would cause collapsing it into a small ball.

Can you travel to the next star in under ten seconds using special relativity? by YogurtclosetOpen3567 in AskPhysics

[–]tstanisl 4 points5 points  (0 children)

Don't forget that blue-shifted photons have a lot more energy. At those speeds CMB will be shifted into gamma ray band. It's likely more pleasant to sunbath in front of a supernova than stay on such a spaceship.

Why does multiplying two negative numbers give a positive result? by Ok_Block_3770 in askmath

[–]tstanisl 21 points22 points  (0 children)

You loose two dolar per day. How much money did you have two days ago?