Re: [fpc-pascal] USB serial interface

2006-12-27 Thread Klaus Hartnegg
 Am Dienstag, 26. Dezember 2006 18:57 schrieb Marco van de Voort:
   The best USB-serial Interfaces for Linux are the ones with FTDI Chip
   inside. http://www.ftdichip.com/
   http://www.ftdichip.com/Products/EvaluationKits/USB-Serial.htm
 
  Do they have the same latency as a normal serial port? I've tried several
  usb-serial ports, and they all have horrible latencies compared to an
  onboard (or PCI card).
 
 May be it is
 also a question of the Kerneldriver and the driver will improve more in the
 future.

It's more likely a general problem of USB.
A real serial port is always ready to receive and it triggers an 

interrupt as soon as it has received a character, so the PC 
immediately fetches the received data.

On USB the converter must buffer all incoming data until
the PC specifically asks it to do its next transfer.
On USB1 there is no guarantee that the PC can do
1000 transfers per second. 
USB1 can transfer a lot more than 1000 bytes per second, 
but not in 1000 transfers of only 1 byte each.
I don't know the exact number for USB2. 
Also it depends on which other USB devides are connected 
because they all share the bus.

Klaus
-- 
Klaus Hartnegg, Brain Research Group, University Freiburg, 
Hansa-strasse 9, D-79104 Freiburg, Germany, [EMAIL PROTECTED]
Please avoid sending me Word attachments (use txt, rtf, or pdf).
See http://www.fsf.org/philosophy/no-word-attachments.html

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]Modifying cpu registers while in turbo pascal interrupt routine ?

2004-03-18 Thread Klaus Hartnegg
 Turbo pascal 7 has the 'interrupt' directive which can be placed behind a
 procedure name to turn it into a interrupt handler.

forget it, it's a bug
it doesn't save all registers (so your program will crash if it uses longint)
and it doesn't provide a stack (so your programm will crash even it it doesn't 
use longint).

 The problem is that these parameters are probably passed 'by value'. So
 these are only copies.

try trapint.pas from this page:
http://www.brain.uni-freiburg.de/~klaus/pascal/sources/

hope this helps,
Klaus
-- 
Klaus Hartnegg, Brain Research Group, University Freiburg, 
Hansa-strasse 9a, D-79104 Freiburg, Germany, [EMAIL PROTECTED]
Please avoid sending me Word attachments (use txt, rtf, or pdf).
See http://www.fsf.org/philosophy/no-word-attachments.html


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] subtract longwords with rollover

2013-11-28 Thread Klaus Hartnegg

Hi,

How can I subtract two longwords such that when the result becomes 
negative, it rolls over?


A := A - B triggers runtime error 201 when the result is negative.

dec (A,B) rolls over, but triggers runtime error 201 when A is higher 
than the highest possible value of longint.


Is dec only declared for longint, but not for longword?

dec(longint(A),B) fails when the result is larger than 2 billion.

I cannot use longint instead of longword, this causes problems in other 
parts of the code.


HELP!

Thanks,
Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] subtract longwords with rollover

2013-11-28 Thread Klaus Hartnegg

Sorry, I forgot to mention there is {$Q-} in the source.

The trick was indeed to use dec(longint(a),b). However I had missed one 
place in the code, so I got another runtime error from there.


But why does it only work with longint, and not with longword?

This looks like a missing declaration of one variant of the dec function.


var
  a,b : longword;
  i,j : byte;

begin
  {$Q-}
  for i := 0 to 4 do begin
 for j := 0 to 4 do begin
write (i,' ',j,'  ');
a := longword(j) * 10;
b := longword(i) * 10;
write (a:10,' ',b:10,' ');
  { a := a-b;  } { fail at 1 0 }
  { dec (a,b); } { fail at 1 0 }
dec (longint(a),b);  { ok }
writeln (a:11);
 end;
  end;
end.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] subtract longwords with rollover

2013-11-29 Thread Klaus Hartnegg

On 28.11.2013 18:32, Tomas Hajny wrote:

On Thu, November 28, 2013 18:12, Klaus Hartnegg wrote:

Sorry, I forgot to mention there is {$Q-} in the source.


I believe that $Q- covers overflow checks, not range checks.


Thanks, this showed me the right direction.

Explanation: I'm trying to port code from Turbo Pascal. There 
word1-word2 does wrap around with {$Q-}{$R+}. And dec(word1) never 
checks for overflow, even with {$Q+}{$R+}.


FreePascal seems to have a wider definition of Range Check, and the 
inc() and dec() functions do check for overflow.


The longint trick prevents runtime error 201, but causes runtime error 
215 if the longword value does not fit into longint.


On closer look this is all behaving like documented, just different from 
TurboPascal (maybe it would have worked in TP-compatibility-mode).


The correct fix is to simply add {$R-} to the code wherever it currently 
has {$Q-}. I have just done this, and now it passes the test.


Klaus

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] quality of FPC random

2015-08-20 Thread Klaus Hartnegg

Am 14.08.2015 um 15:38 schrieb Xiangrong Fang:

I need to generate random numbers to be used as IV of block ciphers.  My
question is: is FPC built-in PRNG good enough as comparing to /dev/urandom?


NO!!!
For crypto always use /dev/urandom


On the other hand, /dev/urandom in my impression is fairly slow, how is
the speed of Random() comparing to that?


Speed is irrelevant, because you do not need many truely random numbers 
for crypto.

For crypto always use /dev/urandom

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] fprecvfrom for IPv6?

2015-08-02 Thread Klaus Hartnegg

Hello,

How can fprecvfrom work with IPv6?

It returns the address of the sender in a record sockaddr, which only 
has space for an IPv4 address.

http://www.freepascal.org/docs-html/rtl/sockets/fprecvfrom.html

Is there an undocumented variant in which psockaddr points to 
sockaddr_in6 instead of sockaddr?


Or is this already the case in the existing fprecvfrom?

One of my programs suddenly claims that fprecvfrom returned an address 
record with size 28 bytes instead of 16 (in fromlen:pSockLen), when it 
runs in Redhat (using xinetd) instead of Ubuntu (using openbsd-inetd), 
when receiving an UDP packet. Interestingly sizeof(sockaddr_in6) is 28. 
Probably not a coincidence, but I whish it were documented, because I 
don't want to develop software based on guessing.


Btw. what does fpcrectfrom do when fromlen^ is smaller than the amount 
of data that it wants to copy to psockaddr^? Buffer overflow?


thanks,
Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] is scrypt available?

2015-10-29 Thread Klaus Hartnegg

Am 27.10.2015 um 18:55 schrieb David W Noon:








Do not use a normal hash function to store passwords. If the password 
file is stolen, the attackers can quickly determine most passwords.


There are special algorithms to securely store passwords. Common 
recommendations are: PBKDF2, bcrypt, scrypt.


Explanation from
https://en.wikipedia.org/wiki/Password_cracking#Prevention

"Many hashes used for storing passwords, such as MD5 and the SHA family, 
are designed for fast computation and efficient implementation in 
hardware. As a result, they are ineffective in preventing password 
cracking, especially with methods like rainbow tables. Using key 
stretching Algorithms, such as PBKDF2, to form password hashes can 
significantly reduce the rate at which passwords can be tested."


See also:
https://en.wikipedia.org/wiki/Key_derivation_function

scrypt for pascal appears to be offered here:
http://www.wolfgang-ehrhardt.de/crchash_en.html
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] howto store passwords

2015-10-30 Thread Klaus Hartnegg

Am 29.10.2015 um 18:08 schrieb Frederic Da Vitoria:

I'd even ask the question: do you really need to store the
passwords? IOW, do you want to be able to send them back to the user? Or
do you only need to check them?


My latest access system does not use passwords at all. The server sends 
the users an email, they must click on a link inside. With password 
there would have to be a plan B in case users forget a password. This is 
typically insecurity-questions, or an email with a link for a password 
reset. This means that whoever can access the emails, can gain access 
anyway. Thus using this as primary access method does not reduce 
recurity. I would argue that in most cases it even improves it.


The best way to store passwords is to not store passwords, not even hashes.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] howto find alternatives for deprecated fucntions?

2015-09-16 Thread Klaus Hartnegg

Hi,

if the docs and the compiler say that a function is deprecated, how can 
I find out which other function should be used instead?


Example: www.freepascal.org/docs-html/rtl/sockets/accept.html says that 
accept is deprecated. Whould I switch to fpaccept eventhough that can do 
a lot less?  I hope that Sock2Text works there as well.


In an ideal world every occurance of the word deprecated in the docs 
would be accompanied by the info which is the alternative.


Btw. the docs for fpaccept contains an example for accept, not for fpaccept.

thanks,
Klaus

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] the accept sample from the manual drives me crazy

2015-09-30 Thread Klaus Hartnegg

Sorry, I meant accept, not access.

Am 30.09.2015 um 11:10 schrieb Klaus Hartnegg:

http://www.freepascal.org/docs-html/rtl/sockets/fpaccept.html

if Accept(S,FromName,Sin,Sout) then
(* WARNING: Accept is deprecated *)
(* ERROR: fromname contains nonsense characters *)



   Sock2Text(S,Sin,Sout);
   reset (Sin);
   rwrite (Sout);
then reset (Sin) crashes or hangs, and it also triggers a deprecated
warning.


My conclusion from these two is that Accept and Sock2Text are more than 
deprecated, they are broken (in version 2.6.4 for Linux).



   filedescr := fpaccept(S, @SAddr, @slen);


How can this filedescr be used other than with fpread?
Can it be implanted into a file of type "text"?
I would prefer to just readln from it.

Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] howto find alternatives for deprecated fucntions?

2015-09-28 Thread Klaus Hartnegg

What can I use instead of Sock2Text? That's also deprecated.

Am 16.09.2015 um 16:50 schrieb Klaus Hartnegg:
www.freepascal.org/docs-html/rtl/sockets/accept.html says that accept 
is deprecated. Whould I switch to fpaccept eventhough that can do a 
lot less?  I hope that Sock2Text works there as well.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Variable Initialization Questions

2016-02-22 Thread Klaus Hartnegg

Am 18.02.2016 um 21:38 schrieb Jonas Maebe:

all global variables are always zeroed.


but it is better to not rely on it because


Local variables are never zeroed


and it is not uncommon that a part from main gets moved to a procedure, 
and all of a sudden you must add commands to initialize the variables. 
It is better to do it always, to avoid surprises.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] fpSelect fails with UDP sockets?

2016-04-01 Thread Klaus Hartnegg

Hello,

Does fpSelect not work with UDP sockets?
It always returns 0, which means it did not see a packet.
Is anything wrong with this code?

Uses
  UnixType, BaseUnix, Sockets;

procedure abort (reason:string);
begin
  writeln (reason);
  halt;
end;

var
  sock: cint;
  adr : {TInet}SockAddr;
  rc  : cint;
  FDS : Tfdset;

begin
  sock := fpSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if sock = -1 then
 abort ('Error from fpSocket');

  adr.sin_family  := AF_INET;
  adr.sin_addr.s_addr := INADDR_ANY;
  adr.sin_port:= htons(53);
  rc := fpBind (sock, @adr, sizeof(adr));
  if rc <> 0 then
 abort ('Error from fpBind');

  Writeln ('send udp packet in less than 5 seconds');
  Fpfd_zero(FDS);
  FpFd_set (sock,FDS);
  rc := fpSelect (1,@FDS,nil,nil,5000);
  if rc = -1 then
 abort ('Error from fpSelect');
  if rc > 0 then
Writeln ('Thank you !')
  else
Writeln ('Too late !');
end.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] SOLVED fpSelect fails with UDP sockets?

2016-04-03 Thread Klaus Hartnegg

It works with
  rc := fpSelect (sock+1,@FDS,nil,nil,5000);

I did not understand the description of the first argument in the docs, 
so I used the value from the sample, this was not a good idea.



For reference here is the source that works:

Uses
  UnixType, BaseUnix, Sockets;

procedure abort (reason:string);
begin
  writeln (reason);
  halt;
end;

var
  sock: cint;
  ladr : SockAddr;
  radr : SockAddr;
  adrlen: tSockLen;
  msg : string[255];
  FDS : Tfdset;
  rc  : cint;

begin
  sock := fpSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if sock = -1 then
 abort ('Error from fpSocket');

  ladr.sin_family  := AF_INET;
  ladr.sin_addr.s_addr := INADDR_ANY;
  ladr.sin_port:= htons(2000);
  rc := fpBind (sock, @ladr, sizeof(ladr));
  if rc <> 0 then
 abort ('Error from fpBind');

  adrlen := sizeof(radr);

  Fpfd_zero (FDS);
  FpFd_set (sock,FDS);
  rc := fpSelect (sock+1,@FDS,nil,nil,5000);
  writeln (rc);
  if rc = -1 then
 abort ('Error from fpSelect');
  if rc = 0 then
 abort ('timeout');

  rc := fpRecvFrom (sock, @msg[1], 255, 0 {flags}, @radr, @adrlen);
  if rc = -1 then
 abort ('Error from FpRecv');

  if rc > 255 then
 rc := 255;
  msg[0] := chr(rc);
  writeln (msg);
end.



And this is the sender:

Uses
  UnixType, BaseUnix, Sockets;

procedure abort (reason:string);
begin
  writeln (reason);
  halt;
end;

var
  sock: cint;
  sadr: {TInet}SockAddr;
  dadr: {TInet}SockAddr;
  adrlen: tSockLen;
  msg : string;
  rc  : cint;

begin
  sock := fpSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if sock = -1 then
 abort ('Error from fpSocket');

  sadr.sin_family  := AF_INET;
  sadr.sin_addr.s_addr := INADDR_ANY;
  sadr.sin_port:= 0; { auto-select }
  rc := fpBind (sock, @sadr, sizeof(sadr));
  if rc <> 0 then
 abort ('Error from fpBind');

  dadr.sin_family := AF_INET;
  dadr.sin_addr.s_bytes[1] := 127;
  dadr.sin_addr.s_bytes[2] := 0;
  dadr.sin_addr.s_bytes[2] := 0;
  dadr.sin_addr.s_bytes[2] := 1;
  dadr.sin_port   := htons(2000);
  adrlen := sizeof(dadr);

  msg := 'hello, world!';

  rc := fpSendTo (sock, @msg[1], length(msg), 0 {flags}, @dadr, adrlen);
  if rc = length(msg) then
 writeln ('ok');
end.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] SOLVED fpSelect fails with UDP sockets?

2016-04-03 Thread Klaus Hartnegg

Am 03.04.2016 um 18:32 schrieb Klaus Hartnegg:

   dadr.sin_addr.s_bytes[1] := 127;
   dadr.sin_addr.s_bytes[2] := 0;
   dadr.sin_addr.s_bytes[2] := 0;
   dadr.sin_addr.s_bytes[2] := 1;


The array index numbers are of course garbage (from incomplete 
copy editing), but somehow it worked nevertheless.


Should have been
  dadr.sin_addr.s_bytes[1] := 127;
  dadr.sin_addr.s_bytes[2] := 0;
  dadr.sin_addr.s_bytes[3] := 0;
  dadr.sin_addr.s_bytes[4] := 1;

Loopback appears to catch the whole loopback network, not just that 
single address.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-23 Thread Klaus Hartnegg

Am 21.02.2017 um 22:12 schrieb James Richters:

 Assign(BitFile,'BitSave.pax');

   {$I-}

 ReWrite(BitFile);

   if ioresult <> 0 then
  writeln ('error opening pax file');

 WriteLn(BitFile,XADJ:1:8);
 WriteLn(BitFile,YADJ:1:8);
 WriteLn(BitFile,ZADJ:1:8);
 WriteLn(BitFile,WADJ:1:8);
 WriteLn(BitFile,AADJ:1:8);
 WriteLn(BitFile,TADJ:1:8);
 WriteLn(BitFile,VADJ:1:8);
 WriteLn(BitFile,UADJ:1:8);
 WriteLn(BitFile,CurrentTool);
 WriteLn(Bitfile,P_Value[4]);

   writeln (bitfile, 'EOF');
   if ioresult <> 0 then
  writeln ('error writing pax file');

 Close(BitFile);

   if ioresult <> 0 then
  writeln ('error closing pax file');
   {$I+}

This writes something at the end of the file that does not come from a 
variable. Next time the error happens, you can check if that is present. 
Then your variables were overwritten in memory. Also it checks for 
errors. And I would disable antivirus or at least tell it to not scan 
this file.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-23 Thread Klaus Hartnegg

Am 22.02.2017 um 21:27 schrieb James Richters:

Here is the procedure that reads the file.


Also you could always try to immediately read the file each time it has 
been modified. Append a log entry to some other file, telling that the 
pax file was modified and whether it was still correct. This would tell 
whether it happens when the file is written, or at some other time.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-03-22 Thread Klaus Hartnegg

Am 21.03.2017 um 19:58 schrieb James Richters:

Does anyone know of a way to force critical data to be written to a SSD so it's 
not lost during a power failure?


Here is a unix-like sync command for windows:
https://technet.microsoft.com/sv-se/sysinternals/bb897438(en-us).aspx
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] FP IDE browse-function (search symbol) errors

2018-06-08 Thread Klaus Hartnegg

Hi,

I'm struggling with two issues with the Search,Symbol (Browse) function 
of the FreePascal IDE.


Are these known issues, or is something wrong with how I installed or 
configured FreePascal?


1: With duplicate names it always shows the results for the one which 
was declared first. Regardless of which the cursor was sitting on when 
triggering the feature. Example:


uses
  a,b;  (* both declare a procedure named 'test' *)

begin
  a.test;
  b.test;
end.

If I inspect b.test, TurboPascal shows that it comes from b, FreePascal 
claims it comes from a.


2: The IDE has a window "Browser Options" which allows for example to 
set the Preferred Pane. But all options there have no effect, the 
changes are not even saved.


Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] round(2.5)=2

2018-06-08 Thread Klaus Hartnegg

Hi,

The round function behaves different than in TurboPascal, and its 
English Wiki page differs from the German page.


When given an argument halfway between to integers, FreePascal rounds to 
the nearest even number, TurboPascal always rounds up (except when it is 
set to mode $N+, but default is $N-).


The German Wiki pages for Round and SetSoundMode (URLs below) contain a 
warning to not use SetRoundMode, because that also affects internal 
calculations, and how numbers are stored in variables when they do not 
fit with full precision.


How serious is this issue compared with using a different rounding method?

The german pages suggest to use this function instead:
function round(x: Float): Integer;  { requires unit math for 'float' }
begin
  if x > 0 then
  round := trunc(x + 0.5)
  else
  round := -trunc(-x + 0.5);
end;

That is fine, except that I will likely forget it in some unit, and 
manually inspecting all units is less reliable than when the compiler 
ensures it.


In Turbo-Pascal the browsing feature (search,symbol) of the IDE can show 
a list of all places where system.round gets called. The FreePascal IDE 
does not show that list (see my other email).


Is there some other reliable(!) way to fix or check this? Maybe a map 
file with all functions that got linked into the executable?


The Wiki pages are
- wiki.freepascal.org/Round
- wiki.freepascal.org/Round/de
- wiki.freepascal.org/SetRoundMode
- wiki.freepascal.org/SetRoundMode/de

If the warning is correct, there should also be a note on
https://www.freepascal.org/docs-html/rtl/math/setroundmode.html

thanks,
Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] round(2.5)=2

2018-06-12 Thread Klaus Hartnegg

Am 12.06.2018 um 09:01 schrieb Mattias Gaertner:

IMHO this is not really relevant, as relying on the exact value of a
real number is erroneous, anyway. So a real number never can be
considered to *exactly* *be* 1.5, but just somewhere nearby, and hence
it's not legal to predict hat it is rounded to.


Floats can represent 1.5 exactly.


Yes. And such numbers do pop up all the time.

That mathematical theory is only valid for measured values. If measured 
with unlimited precision.


Then you could arbitrarily define round(1.5)=9, and nobody would ever 
notice. Because the chance would be zero that a variable ever has that 
exact value.


But we do not only store measured values in variables. And we often 
measure with low precision. That's why this is relevant.


When I got different results from the FreePascal port of my source, it 
took me quite a while until I found the cause in the compiler. Because 
that is typically not the first place to look at.


Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] round(2.5)=2

2018-06-12 Thread Klaus Hartnegg

Am 12.06.2018 um 23:24 schrieb Jonas Maebe:

On 12/06/18 23:12, Klaus Hartnegg wrote:
This behaviour does have advantages in the special case of adding up 
many rounded numbers, but it ruins the graphics output of my software. 
It should at least be configurable. And in {$mode tp} it should behave 
like Turbo Pascal, because that's what this mode is made for.


The basic issue is that FPC doesn't support the 6 byte real type used by 
TP in {$n-}, nor the associated behaviours such as rounding, 
range/precision cut-offs etc. FPC indeed can only support TP {$n+} mode 
compatibility at this time (and even that is not always exact, as it 
depends on the FPU precision support by the hardware and OS).


Nevertheless it could offer an option to use the other rounding method.

Yes, there would still be deviations in some math results, because of 
the different precision. But these deviations are much smaller than 
those from the other rounding method.


Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] round(2.5)=2

2018-06-12 Thread Klaus Hartnegg

Am 11.06.2018 um 10:01 schrieb Michael Schnell:
 - the way of rounding is defined by the CPU hardware, even old Turbo 
Pascal executables do banker's rouding on modern hardware.


No, it does not depend on the hardware, but on the setting of $N. Turbo 
Pascal with $N+ rounds like FreePascal. But the default is $N-. In this 
mode Turbo Pascal always rounds up.


var
  a : real;
begin
  a := 35;
  writeln (round(a*1.1));
end.

FreePascal: 38
Turbo Pascal: 39

And when the compiler precalculates a constant expression, it always 
rounds up as well, regardless of $N.


Nobody expects that increasing a number by 10% will depend on whether 
the resulting number will be close to an even integer. That feels 
completely erratic.


This behaviour does have advantages in the special case of adding up 
many rounded numbers, but it ruins the graphics output of my software. 
It should at least be configurable. And in {$mode tp} it should behave 
like Turbo Pascal, because that's what this mode is made for.


  - SetRoundMode is not only dangerous, but does not help, anyway, as 
there is no mode defining the "intuitive " "non-banker rouding" method.


That's why I'm looking for a better solution. The the only *reliable* 
solution appears to be compiling a modified version of FreePascal.


Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] howto print hires-graphics in windows?

2018-05-03 Thread Klaus Hartnegg

Hi,

I want to draw simple line graphics both on screen and printer (full 
page, with ~300dpi).


What is the easiest way to achieve this in FreePascal (not Lazarus)?
Maybe a graph-unit that outputs to GDI?

thanks,
Klaus
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Prorammatically sending file using HTTP POST?

2018-05-03 Thread Klaus Hartnegg

Am 04.04.2018 um 13:50 schrieb Bo Berglund:

By the way, I am using the Indy10 components via the indylaz package.
Lazarus is 1.8.0 and FPC 3.0.4 on Windows7.


If anybody wants to do this just with winsock:


const
  dstport = 80;
  http_path = '/post.php';

type
  Int32 = Longint;


function wordstr (value:word) : string;
var
  c : string;
begin
  str (value,c);
  wordstr := c;
end;


procedure SendHttpPost (c:ansistring;
var msg:string);

var
  wsadata : TWSAData;  // winsock

var
  sock : TSocket;
  addr : sockets.sockaddr_in;
  numbytes : Int32;
  sendstring : ansistring;
  rc : longint;
  ok : boolean;

begin
  // initialize winsock
  if (WSAStartup($0202, wsadata) <> 0) then begin
 msg := 'cannot initialize winsock';
 exit;
  end;

  // get socket
  sock := fpSocket(AF_INET, SOCK_STREAM, 0);
  if (sock = -1) then begin
 msg := 'cannot retrieve socket';
 exit;
  end;


  addr.sin_family := AF_INET;
  addr.sin_port := htons(dstport);
  GetServerIP (addr.sin_addr, ok);

  rc := fpConnect (sock, @addr, sizeof(addr));
  if rc <> 0 then begin
 msg := 'cannot connect to host';
 exit;
  end;

  // c := stringreplace (c,' ','&',[rfReplaceAll]);

  // send data
  sendstring := 'POST '+http_path+' HTTP/1.1'+#13+#10
  + 'host: '+NetAddrToStr(addr.sin_addr)+#13+#10
  + 'Content-Type: application/x-www-form-urlencoded'+#13+#10
  + 'Content-Length: '+wordstr(length(c))+#13+#10
  + #13+#10
  + c;

  numbytes := fpSend(sock, @sendstring[1], length(sendstring), 0);
  if (numbytes = SOCKET_ERROR) then begin
 msg := 'cannot send data to host';
  end else begin
 msg := 'Sent '+wordstr(numbytes)+' bytes to 
'+NetAddrToStr(addr.sin_addr));

  end;

  // clean up
  closesocket(sock);

  // shut down winsock
  WSACleanup;
end;
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal