Author Topic: When does C require 10.0 instead of 10 when dealing with float or double?  (Read 20129 times)

0 Members and 3 Guests are viewing this topic.

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1906
  • Country: se
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #50 on: November 10, 2022, 10:42:38 pm »
Try reading a book on C, seriously. Don't just make stuff up.
Or find a good tutor, or follow a course - I think I also suggested this a couple of times already.

Relying on assumptions is bound to leave grey areas, and we can't possibly clear them all* as we can't know what you don't know.
This where a good book can help (not suggesting to use the standard - as much it is my go-to reference - cppreference.com is a more down-to-earth and still accurate** substitute).

Another good resource are the secure programming oriented SEI-CERT rules and recommendation. Reading and understanding them gives a good insight on most of C pitfalls.

The willingness to delve deeper is there, and that's really good.
But I sometimes feel it tries to unveil inconsequential things or connect to past experiences that might or might not be relevant (e.g. discussing about BSS, DATA and COMMON is usually not important, unless there is a very specific need).

* As a community, I would say the regulars here are very keen to help, with variable levels of grumpiness.
** Never found a fault there, whereas I did a couple of times in the SEI-CERT rules (C++, corrections were quick once I convinced them).
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1906
  • Country: se
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #51 on: November 10, 2022, 10:54:13 pm »
Or, uh... use Rust? :-DD
I would not know  :-//
If even Linus Torvalds himself is admitting Rust code in the kernel, it might not be just another fad.
We just finished running a pilot I suggested -we had an intern rewrite a newly C implemented small feature using Rust - and the results were better than expected: slightly slower compilation, very similar speed except for an outlier where Rust was 500% faster than C, worse memory occupation.

I should put some serious effort in learning it.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline KaneTW

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: de
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #52 on: November 10, 2022, 11:11:38 pm »
Use Rust and you won't have to worry about intransparent typing nonsense.

Read the C standard and you'll know how the compiler ought to behave.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #53 on: November 10, 2022, 11:12:49 pm »
Rust still has promotion rules. And while they are logical if you look at them as a whole, they may be surprising in some cases. So, reading the spec is still required of you will be in the same exact situation.

But vendor supplied libraries are not going to be in Rust for a long time, if ever.
« Last Edit: November 10, 2022, 11:15:23 pm by ataradov »
Alex
 

Offline KaneTW

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: de
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #54 on: November 10, 2022, 11:16:23 pm »
Last time I used rust, it had no implicit type conversion. You had to convert eg i16 to i32 using "as."
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #55 on: November 10, 2022, 11:24:38 pm »
For whatever reason I thought this proposal https://internals.rust-lang.org/t/implicit-widening-polymorphic-indexing-and-similar-ideas/1141 was actually implemented, since it makes sense.

But yes, it looks like it was just a discussion and was not implemented in any way.

This actually sucks. Like yes, it is "safe" on the technical level, but logically you just make programmer do more work and potentially more logical mistakes.
« Last Edit: November 10, 2022, 11:34:52 pm by ataradov »
Alex
 

Offline tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #56 on: November 10, 2022, 11:31:45 pm »
Try reading a book on C, seriously. Don't just make stuff up.

Just so.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline KaneTW

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: de
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #57 on: November 10, 2022, 11:58:09 pm »
For whatever reason I thought this proposal https://internals.rust-lang.org/t/implicit-widening-polymorphic-indexing-and-similar-ideas/1141 was actually implemented, since it makes sense.

But yes, it looks like it was just a discussion and was not implemented in any way.

This actually sucks. Like yes, it is "safe" on the technical level, but logically you just make programmer do more work and potentially more logical mistakes.

I (mostly) don't agree. It adds extra notation but being explicit about your types saves so much pain in the long run.

The way Rust does it does seem to invite potential for error -- ideally you'd have a cheap widening operation that's guaranteed to work, an overflowing or saturating narrowing operation that's also guaranteed to work, and a narrowing operation that returns an error value if the input is incompatible with the target type.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17773
  • Country: fr
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #58 on: November 10, 2022, 11:59:58 pm »
Yep. Still sad to see how many people using C never actually learned it. But we keep saying that and saying it won't change a thing...

And now sure C has its quirks, but most other languages have their own.

To be fair, I do think the OP would be a typical developer that would benefit from using Oberon, the maintainer of a set of commercial tools is on this forum (Astrobe). Now the problem IMO is that (as many other people) the OP is largely relying on third-party libs that may not be available in any other language than C. I do not know if Astrobe has any kind of Ethernet library, for instance. But if so, the maintainer can chime in.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #59 on: November 11, 2022, 12:21:33 am »
Or, uh... use Rust? :-DD

Sure. In which case: read a book on Rust. It's different to C.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #60 on: November 11, 2022, 01:37:08 am »
Oberon

Porting Oberon  to MIPS5+ is *very* challenging.

 
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #61 on: November 11, 2022, 01:39:47 am »
Last time I used rust, it had no implicit type conversion. You had to convert eg i16 to i32 using "as."

exactly what my-C does!
exactly what DO178B requires!
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17773
  • Country: fr
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #62 on: November 11, 2022, 01:42:39 am »
Oberon

Porting Oberon  to MIPS5+ is *very* challenging.

I can't see why it would be.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11140
  • Country: fi
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #63 on: November 11, 2022, 05:27:36 pm »
Try reading a book on C, seriously. Don't just make stuff up.
Or find a good tutor, or follow a course - I think I also suggested this a couple of times already.

Or, even simpler, whenever you find yourself making assumptions without knowing, just Google it. 99% of time a Stack Overflow discussion pops up, and 99% of the time it's at least 99% correct. Much better than guessing, and less effort than trying to find it from the standard or from a book.

That's a good rule of thumb anyway - don't assume, check. It is tempting to think it saves time to assume, but the opposite is true. Googling some C detail takes 1-2 minutes, and assuming seems to save that much, but ends up wasting countless of hours by banging head into wall.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #64 on: November 11, 2022, 07:50:38 pm »
Or, uhh, use my-c  :-DD

Well. It's now able to recompile itself.
my-c compliled with gcc, outputs my-c-cc1
my-c-cc1 is able to compile my-c-cc2
my-c-cc2 is able to compile my-c-cc2

Keep getting better  :o :o :o
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17773
  • Country: fr
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #65 on: November 11, 2022, 07:58:28 pm »
Yes, this is bootstrapping. Glad you have managed to design a language (well, looks like a variant of C) that works well for you.

So, why would Oberon be hard to implement for a target for which C works well? The programming model is pretty similar.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 5083
  • Country: gb
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #66 on: November 11, 2022, 11:13:15 pm »
why would Oberon be hard to implement for a target for which C works well? The programming model is pretty similar.

most of the my-c implementation comes from the libraries used for ICE and has been integrated with monads, that's something I use on a daily basis, so it's easier.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1906
  • Country: se
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #67 on: November 12, 2022, 12:29:58 am »
comes from the libraries used for ICE
Beware! Given current and foreseen global warming, all your code is at risk!
Nandemo wa shiranai wa yo, shitteru koto dake.
 
The following users thanked this post: Siwastaja

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17773
  • Country: fr
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #68 on: November 12, 2022, 06:48:36 pm »
Yeah, OK...  ::)
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 5967
  • Country: gb
  • Doing electronics since the 1960s...
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #69 on: November 14, 2022, 11:05:35 am »
Quote
Or find a good tutor, or follow a course - I think I also suggested this a couple of times already.

I used K&R to learn C.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: When does C require 10.0 instead of 10 when dealing with float or double?
« Reply #70 on: November 14, 2022, 11:24:35 pm »
Quote
Or find a good tutor, or follow a course - I think I also suggested this a couple of times already.

I used K&R to learn C.

1st edition (1978) is short (226 pages plus index) but obsolete.

2nd edition (1988) is still only 260 pages, and is essentially close enough for the basics of modern C (though missing things such as stdint.h, introduced in C99).

However your posts here are incompatible with having read (and understood) either edition. e.g. section 2.7 "Type Conversions" in 2nd edition or section 6 in Appendix A in 1st edition.
 
The following users thanked this post: langwadt


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf