Forum > FPC development

[SOLVED]Confusion with signed types: is ( 256 * 1 = 0 ) a design issue or a bug?

<< < (2/3) > >>

Thaddy:
A PShortInt is a byte size. In your case that is really the cause. It must be PSmallint.

Do not get discouraged..

We are always available to help.
After reading your explanation there are more options to do it in Pascal in a proper way.
But in your example, note my remarks, because that fixes it.

marcov:
(note that a simple way of avoiding what is short and what is small, is to use int8,int16 and their corresponding pointer sizes. They have been in FPC for quite some time.

As to why type is not promoted, I can vaguely remember this was a TP/early delphi "feature" to use partial (AH/AL or AX vs EAX) to gain speed. FPC got it somewhere during the 2.0 or 2.2 cycle afaik.  One of those that are not documented but only found when people start submitting bug reports with code that differs in results between Delphi and FPC.

Thaddy:
It does not differ between Delphi and FPC: you must use the correct type.

marcov:

--- Quote from: Thaddy on November 05, 2024, 04:47:49 pm ---It does not differ between Delphi and FPC: you must use the correct type.

--- End quote ---

Afaik it did in older FPC versions.

old_DOS_err:
Thanks to the dialogue with Thaddy, I had a moment of clarity, rather rare these days (the problem with getting old is sometimes you don't realise how heavily you've forgotten something until it walks back up to you holding a wet fish and slaps you around the face with it). I had totally forgotten the basics of how computers deal with negative numbers.

Have just run a full auto test on the unsigned and, more importantly, the signed, both positive and negative, up to 4 byte types. Finally got all the bugs out. Still to test Int64, have already ran the QWord test (had to use steps for that, was taking way too long to do EVERY number), but the mechanics are all the same as the other signed, the only interesting number is going to be max negative (because I build the number as a positive, basically absolute, then negate on last digit, if converting a max negative, say converting a string to a byte that has '-128', have to add and negate at the same time, := ( 28 + 100 ) * -1, since 128 stored into Int8 will give an error or be wrong value if range turned off).

The main thing is that I now have a working string number converter that replaces the whole range of string to int functions and good old Val, that will accept a number with commas and give a boolean return if an invalid number.

Thanks marcov for the contribution. I really only use assembler in pascal when doing bit work, as I know this potentially makes the code way too hardware dependent (because I haven't got into porting to other platforms, that is a world of pain I have yet to experience). My Turbo Pascal days are long behind me, but up until generics, my linked list class was using sort of mock polymorphism using pointers, written in TP back in the 90s. I did end up using Int8 and ^Int8, etc. One of the things I also seemed to experience, which I have seen noted in the free pascal documentation, though not quite connected, is that even if 2 variables are effectively the same type, but have different names, it can cause issues.

For those interested, the reason why signed and unsigned cannot be treated the same is that the right most bit is effectively the sign, rather similar to UTF8, once that bit is set to 1 the number is then negative. That is what I forgot, oops. It is about the only downside of placing a function into a library, trying to cater for the range of types.

Thanks for all the responces, good to get clarity when getting into a pickle.

Phil

p.s. I couldn't find the signature part in the forum profile, did search for it, the entry I found when someone asked that question, gave an option that wasn't on my screen. So can some please tell me the fool proof way to add the below bit to the signature section. Also couldn't see where I mark this post as solved.


Program Details:

Lazarus: 3.4 (date: 2024-05-25)   FPC: 3.2.2   Version: Win64
Operating system: Windows Pro 10, 22H2
Hardware: Intel Core i5-3570K CPU, 3.40GHz, RAM: 8GB, 64 bit

Bio (give a take a year or 2):
1980s: commercial programmer in small companies, self taught, including assembler (main language dBASEIII)
1990s: Computing degree, introduced to Turbo Pascal, loved it, final year project: my own programming language
Took a long break from computing
2018: returned to hobby programming, found Free Pascal and Lazarus (hooray, didn't have to use C++)
Write mostly file management programs, done a few programs for friends (may one day put stuff on a website)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version