-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DNEGATE doesn't in v2.2 #13
Comments
Hi RigTig, |
Hi RigTig,
Thanks again for reporting this issue :-) Regards & Greetings from Germany, |
Thomas,
Many thanks for fix. All I'd done was figure the high byte was too high by
1, so my temporary fix was just
: DNEGATE DNEGATE 1- ;
anyway, just so you have an integer square root when you need it, here is
one algorithm coded in eForth
\ some maths utility words
\ eForth + is u+
: u+ ( n n -- u )
+
;
: dnegate ( d -- -d )
\ fix for bug in v2.2
dnegate 1-
;
: ud+ ( ud ud -- ud )
( l h l h )
r rot um+ ( h d ) rot u+ ( d ) r> u+
;
: d- ( d d -- d )
\ 2do: ensure works on full range
dnegate ud+
;
: d> ( d d -- f )
d- swap drop 0< not
;
: isqrt ( d -- n )
$8000 ( d c ) $8000 ( d c g )
begin
dup dup um* ( d c g g^2)
6 pick 6 pick ( d c g g^2 d )
d> if ( d c g ) over xor then ( d c g )
swap 2/ $7fff and ( d g c )
dup 0= if
drop rot rot 2drop -1 ( g true )
else
swap over ( d c g c ) or 0
then
until
;
The only trick here is to do a right bit shift by one bit, I've used the 2/
(which accepts positive and negative integers) and co-opted it by just
dropping the sign bit ( $7fff and ).
During my testing, I had the following line just after the 'begin'
cr 2dup . . \ g in col1 and c in col 2
Interesting to watch how the result gets refined.
For the record, here is the C program I used as the source of the algorithm
unsigned int sqrt32(unsigned long n)
{
unsigned int c = 0x8000;
unsigned int g = 0x8000;
for (;;) {
if (g*g > n) {
g ^= c; # Bitwise exclusive OR
}
c >>= 1; # Right bit shift
if (c == 0) {
return g;
}
g |= c; # Bitwise inclusive OR
}
}
Please keep up the good work
Regards, RigTig
…On Mon, Feb 13, 2017 at 4:54 AM, Thomas ***@***.***> wrote:
Closed #13 <#13>.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#13 (comment)>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AWVRezIkQan1xwjHG3pxhqKmRQtclvDjks5rb0c4gaJpZM4L-d__>
.
|
Hej RigTig, I tested the code you posted: it works great, but Would it be OK to add the code to a |
Thomas,
Be my guest. It is the least I can do given that I got the algorithm from the net and your efforts underlie what I did anyway. Well spotted about the shortcoming.
I am working on a simple GCODE interpreter for a 3D printer and I think it'll just fit onto STM8. If so, I'll be using an ESP14. I have thoroughly enjoying exploring the inner workings of stm8ef.
Thank you for your public contributions too.
Regards, RigTig
… On 20 Feb 2017, at 07:19, Thomas ***@***.***> wrote:
Hej RigTig,
I tested the code you posted: it works great, but 256 0 isqrt . returns 15, clearly a rounding error. Adding the line 1 0 ud+ to isqrt fixes this :-)
Would it be OK to add the code to a lib section in the STM8EF repo under a (permissive) OS license?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Thanks :-) |
Tested the isqrt and confirmed the bug. A little bit of exploring found the culprit: d> was actually d>=. So here is a new d> that seems to be ok now, and you'd never guess that isqrt actually gives 16 as the answer for 256 0 isqrt . : d> ( d d -- f ) |
Great, thanks! I had chiefly looked in the same direction but I falsely assumed that As the GitHub issues appear to work great as a discussion platform! Here is something that might be relevant for your G-code project and also for trigonometry & fractional arithmetics:
I've got no experience with fractional arithmetics (only fixed point), I guess that I should start experimenting a bit. CORDIC used to be widely used (there is even a MCS51 µC, the XC886, with CORDIC hardware for field oriented motor control). Another that needs solving is a "uploader" program, ideally one that does includes, and that attempts to be smart (i.e. one that tests whether a library has already been loaded). AmForth has an interesting solution, but I'm skeptical about doing constant replacement in the uploader. |
\ I am having great fun using eForth
\ thank you for a great piece of work
\ In v2.2
3 0 dnegate .s \ results in
FFFD 0 <sp ok
\ when it should be FFFD FFFF <sp ok
\ regards, RigTig
The text was updated successfully, but these errors were encountered: