* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
@ 2017-08-21 20:51 Keith David Bershatsky
2017-08-21 21:30 ` Noam Postavsky
0 siblings, 1 reply; 12+ messages in thread
From: Keith David Bershatsky @ 2017-08-21 20:51 UTC (permalink / raw)
To: Alan Third; +Cc: emacs-devel
Thank you, Alan:
I had erroneously assumed that `%f` was only for adding/removing decimal places or rounding, rather than to show the approximate value of a `double` in this context.
I ran a few tests and am guessing that `%f` uses six (6) decimal places. I understand that %f can be used to increase/decrease the number of decimal places. However, this leaves me guessing exactly what the double looks like. If the double is exactly 1.0, then it would be nice to see the same thing rather than 1.000000. If the double is several decimal places, then I'd like to see the whole thing.
Then again, it is possible that the snippet in the opening thread is producing a double of exactly six (6) decimal places, but I do not know for sure if that is the case.
So the `%f` solution give me 99% of what I was looking for, which is certainly sufficient to handle this particular issue. If you or anyone else have a way to print the exact value of whatever the double is (without adding/removing decimal places), an additional approach would be appreciated.
Keith
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATE: [08-21-2017 13:07:56] <21 Aug 2017 21:07:56 +0100>
FROM: Alan Third <alan@idiocy.org>
>
> * * *
>
> Is it not just like printf?
>
> NSTRACE("float: %f", var);
>
> --
> Alan Third
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
2017-08-21 20:51 How to use a float value with either GLYPH_DEBUG or NSTRACE Keith David Bershatsky
@ 2017-08-21 21:30 ` Noam Postavsky
2017-08-21 23:55 ` Paul Eggert
0 siblings, 1 reply; 12+ messages in thread
From: Noam Postavsky @ 2017-08-21 21:30 UTC (permalink / raw)
To: Keith David Bershatsky; +Cc: Alan Third, Emacs developers
On Mon, Aug 21, 2017 at 4:51 PM, Keith David Bershatsky <esq@lawlist.com> wrote:
> Thank you, Alan:
>
> I had erroneously assumed that `%f` was only for adding/removing decimal places or rounding, rather than to show the approximate value of a `double` in this context.
>
> I ran a few tests and am guessing that `%f` uses six (6) decimal places. I understand that %f can be used to increase/decrease the number of decimal places. However, this leaves me guessing exactly what the double looks like. If the double is exactly 1.0, then it would be nice to see the same thing rather than 1.000000. If the double is several decimal places, then I'd like to see the whole thing.
>
> Then again, it is possible that the snippet in the opening thread is producing a double of exactly six (6) decimal places, but I do not know for sure if that is the case.
>
> So the `%f` solution give me 99% of what I was looking for, which is certainly sufficient to handle this particular issue. If you or anyone else have a way to print the exact value of whatever the double is (without adding/removing decimal places), an additional approach would be appreciated.
I think `%.20g' should give you that (the max precision of double is
about 17 decimal digits), although I would imagine that "the whole
thing" is usually more than is really useful.
https://www.gnu.org/software/libc/manual/html_node/Floating_002dPoint-Conversions.html
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
2017-08-21 21:30 ` Noam Postavsky
@ 2017-08-21 23:55 ` Paul Eggert
0 siblings, 0 replies; 12+ messages in thread
From: Paul Eggert @ 2017-08-21 23:55 UTC (permalink / raw)
To: Noam Postavsky, Keith David Bershatsky; +Cc: Alan Third, Emacs developers
On 08/21/2017 02:30 PM, Noam Postavsky wrote:
> I would imagine that "the whole
> thing" is usually more than is really useful.
Yes, %.20g is not what is wanted here as it will output trailing excess
digits, e.g, it outputs 0.1 as "0.10000000000000000555". What's wanted
is the minimum number of digits that does not lose information. You can
use dtoastr to do that, which is what number-to-string does.
E.g., something like the following (untested) C code. Although this
assumes CGFloat is 'double', and outputs excess precision on 32-bit
platforms where CGFloat is 'float', it would be easy to fix that if you
like the idea.
#include <ftoastr.h>
void
example (CGFloat value)
{
char buf[DBL_BUFSIZE_BOUND];
NSTRACE ("float: %s", dtoastr (buf, sizeof buf, 0, 0, value));
}
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
@ 2017-08-22 17:04 Keith David Bershatsky
0 siblings, 0 replies; 12+ messages in thread
From: Keith David Bershatsky @ 2017-08-22 17:04 UTC (permalink / raw)
To: Anders Lindgren, Noam Postavsky; +Cc: Alan Third, Paul Eggert, emacs-devel
I have tested Paul's amended example provided by Noam, and can confirm that it works well.
I have also implemented the example of Anders, which works well without any changes needed.
Thank you all for teaching me how to get more out of the built-in trace debugging tools.
Greatly appreciated,
Keith
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
@ 2017-08-22 4:30 Keith David Bershatsky
2017-08-22 8:23 ` Anders Lindgren
0 siblings, 1 reply; 12+ messages in thread
From: Keith David Bershatsky @ 2017-08-22 4:30 UTC (permalink / raw)
To: Paul Eggert; +Cc: Alan Third, Noam Postavsky, emacs-devel
Thank you, Paul, for the suggestion to use dtoastr. I tried that and a few variations, but got stuck because dtoastr returns an `int` and `%s` expects a `char` value. Substituting `%s` for `%d` did not yield the correct results.
Keith
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATE: [08-21-2017 16:55:08] <21 Aug 2017 16:55:08 -0700>
FROM: Paul Eggert <eggert@cs.ucla.edu>
>
> * * *
>
> E.g., something like the following (untested) C code. Although this
> assumes CGFloat is 'double', and outputs excess precision on 32-bit
> platforms where CGFloat is 'float', it would be easy to fix that if you
> like the idea.
>
> #include <ftoastr.h>
>
> void
> example (CGFloat value)
> {
> char buf[DBL_BUFSIZE_BOUND];
> NSTRACE ("float: %s", dtoastr (buf, sizeof buf, 0, 0, value));
> }
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
2017-08-22 4:30 Keith David Bershatsky
@ 2017-08-22 8:23 ` Anders Lindgren
2017-08-22 11:32 ` Noam Postavsky
0 siblings, 1 reply; 12+ messages in thread
From: Anders Lindgren @ 2017-08-22 8:23 UTC (permalink / raw)
To: Keith David Bershatsky
Cc: Alan Third, Paul Eggert, emacs-devel, Noam Postavsky
[-- Attachment #1: Type: text/plain, Size: 2009 bytes --]
Hi!
I think(*) you can use "%a" to print the hexadecimal representation of a
floating point number. This prints the floating point number exactly, down
to the last bit in the mantissa. It looks like 0x0.3p10, where 0x0.3 is the
"significant" part and "p10" the binary exponent, and it represents the
number 0x0.3 * 2^10.
In the tradition of NSTRACE, you can define a macro for the format string
(to ensure that all functions that print colors use the same format). For
example:
#define NSTRACE_FMT_RGB "(R:%a G:%a B:%a)"
And another to push the arguments:
#define NSTRACE_ARG_RGB(color) (color).redComponent,
(color).greenComponent, (color).blueComponent
With this, you should be able to print the color values ergonomically using:
NSTRACE("The colors are " NSTRACE_FMT_RGB,
NSTRACE_ARG_RGB([background_color colorUsingDefaultColorSpace]));
To make this even shorter, you can define the macro "NSTRACE_RGB" along the
lines of "NSTRACE_SIZE" and "NSTRACE_POINT".
* Currently, I can't test this, as I have no mac nearby.
-- Anders
On Tue, Aug 22, 2017 at 6:30 AM, Keith David Bershatsky <esq@lawlist.com>
wrote:
> Thank you, Paul, for the suggestion to use dtoastr. I tried that and a
> few variations, but got stuck because dtoastr returns an `int` and `%s`
> expects a `char` value. Substituting `%s` for `%d` did not yield the
> correct results.
>
> Keith
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> DATE: [08-21-2017 16:55:08] <21 Aug 2017 16:55:08 -0700>
> FROM: Paul Eggert <eggert@cs.ucla.edu>
> >
> > * * *
> >
> > E.g., something like the following (untested) C code. Although this
> > assumes CGFloat is 'double', and outputs excess precision on 32-bit
> > platforms where CGFloat is 'float', it would be easy to fix that if you
> > like the idea.
> >
> > #include <ftoastr.h>
> >
> > void
> > example (CGFloat value)
> > {
> > char buf[DBL_BUFSIZE_BOUND];
> > NSTRACE ("float: %s", dtoastr (buf, sizeof buf, 0, 0, value));
> > }
>
>
[-- Attachment #2: Type: text/html, Size: 2753 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
2017-08-22 8:23 ` Anders Lindgren
@ 2017-08-22 11:32 ` Noam Postavsky
2017-08-22 11:51 ` Anders Lindgren
2017-08-22 15:47 ` Paul Eggert
0 siblings, 2 replies; 12+ messages in thread
From: Noam Postavsky @ 2017-08-22 11:32 UTC (permalink / raw)
To: Anders Lindgren
Cc: Alan Third, Keith David Bershatsky, emacs-devel, Paul Eggert
On Tue, Aug 22, 2017 at 4:23 AM, Anders Lindgren <andlind@gmail.com> wrote:
> I think(*) you can use "%a" to print the hexadecimal representation of a
> floating point number. This prints the floating point number exactly, down
> to the last bit in the mantissa. It looks like 0x0.3p10, where 0x0.3 is the
> "significant" part and "p10" the binary exponent, and it represents the
> number 0x0.3 * 2^10.
I think this is GNU libc specific, also not as readable for humans.
> On Tue, Aug 22, 2017 at 6:30 AM, Keith David Bershatsky <esq@lawlist.com>
> wrote:
>>
>> Thank you, Paul, for the suggestion to use dtoastr. I tried that and a
>> few variations, but got stuck because dtoastr returns an `int` and `%s`
>> expects a `char` value. Substituting `%s` for `%d` did not yield the
>> correct results.
dtoastr returns the length of the string like *printf, so you would
have to use it like this:
void
example (CGFloat value)
{
char buf[DBL_BUFSIZE_BOUND];
dtoastr (buf, sizeof buf, 0, 0, value)
NSTRACE ("float: %s", buf);
}
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
2017-08-22 11:32 ` Noam Postavsky
@ 2017-08-22 11:51 ` Anders Lindgren
2017-08-22 15:47 ` Paul Eggert
1 sibling, 0 replies; 12+ messages in thread
From: Anders Lindgren @ 2017-08-22 11:51 UTC (permalink / raw)
To: Noam Postavsky
Cc: Alan Third, Keith David Bershatsky, emacs-devel, Paul Eggert
[-- Attachment #1: Type: text/plain, Size: 923 bytes --]
Hi!
On Tue, Aug 22, 2017 at 1:32 PM, Noam Postavsky <
npostavs@users.sourceforge.net> wrote:
> On Tue, Aug 22, 2017 at 4:23 AM, Anders Lindgren <andlind@gmail.com>
> wrote:
>
> > I think(*) you can use "%a" to print the hexadecimal representation of a
> > floating point number. This prints the floating point number exactly,
> down
> > to the last bit in the mantissa. It looks like 0x0.3p10, where 0x0.3 is
> the
> > "significant" part and "p10" the binary exponent, and it represents the
> > number 0x0.3 * 2^10.
>
> I think this is GNU libc specific, also not as readable for humans.
>
It's part of the C99 standard, see paragraph 7.19.6.1.
True, that it's not as readable. However, in this case it appeared
important to see the exact value. One approach might be to print it both as
a normal number and as a hexadecimal number, after all it is only used for
trace output when debugging the NS port.
-- Anders
[-- Attachment #2: Type: text/html, Size: 1455 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
2017-08-22 11:32 ` Noam Postavsky
2017-08-22 11:51 ` Anders Lindgren
@ 2017-08-22 15:47 ` Paul Eggert
1 sibling, 0 replies; 12+ messages in thread
From: Paul Eggert @ 2017-08-22 15:47 UTC (permalink / raw)
To: Noam Postavsky, Anders Lindgren
Cc: Alan Third, Keith David Bershatsky, emacs-devel
On 08/22/2017 04:32 AM, Noam Postavsky wrote:
> dtoastr returns the length of the string like *printf, so you would
> have to use it like this:
>
> void
> example (CGFloat value)
> {
> char buf[DBL_BUFSIZE_BOUND];
> dtoastr (buf, sizeof buf, 0, 0, value)
> NSTRACE ("float: %s", buf);
> }
Thanks for correcting the example. Sorry, but I can no longer easily
test GNUstep code since Fedora stopped carrying GNUstep.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: How to use a float value with either GLYPH_DEBUG or NSTRACE
@ 2017-08-21 21:45 Keith David Bershatsky
0 siblings, 0 replies; 12+ messages in thread
From: Keith David Bershatsky @ 2017-08-21 21:45 UTC (permalink / raw)
To: Noam Postavsky; +Cc: Alan Third, emacs-devel
Thank you, Noam:
Yes, that works nicely to eliminate the extra zeros and it also lets me see several decimal places when they have a value.
Thank you also for the link to the floating point conversions page.
Greatly appreciated,
Keith
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATE: [08-21-2017 14:30:52] <21 Aug 2017 17:30:52 -0400>
FROM: Noam Postavsky <npostavs@users.sourceforge.net>
>
> * * *
>
> I think `%.20g' should give you that (the max precision of double is
> about 17 decimal digits), although I would imagine that "the whole
> thing" is usually more than is really useful.
>
> https://www.gnu.org/software/libc/manual/html_node/Floating_002dPoint-Conversions.html
^ permalink raw reply [flat|nested] 12+ messages in thread
* How to use a float value with either GLYPH_DEBUG or NSTRACE
@ 2017-08-21 17:07 Keith David Bershatsky
2017-08-21 20:07 ` Alan Third
0 siblings, 1 reply; 12+ messages in thread
From: Keith David Bershatsky @ 2017-08-21 17:07 UTC (permalink / raw)
To: Emacs Devel
The following snippet yields the correct the result. However, I would like to see the float values for RED, BLUE, and GREEN in the terminal when running NSTRACE and/or GLYPH_DEBUG. I believe that I am limited to things that FORMAT accepts, however, I cannot figure out how to incorporate a float value in a NSTRACE or/or GLYPH_DEBUG statement.
It would be greatly appreciated if someone could please give me an example of how to use a float value as an argument for either GLYPH_DEBUG or NSTRACE.
BACKGROUND: I am working on my own feature requests (Emacs Bug #17684 and #22873).
struct frame *f = XFRAME (WINDOW_FRAME (w));
struct face *face;
/* The default face, possibly remapped. */
face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
NSColor *background_color = ns_lookup_indexed_color (face->background, f);
Lisp_Object color_vector;
EmacsCGFloat red, green, blue, alpha, gray;
char buf[1024];
const char *str;
if ([[background_color colorSpaceName] isEqualToString: NSNamedColorSpace])
if ((str =[[background_color colorNameComponent] UTF8String]))
{
color_vector = build_string ((char *)str);
goto done;
}
[[background_color colorUsingDefaultColorSpace]
getRed: &red green: &green blue: &blue alpha: &alpha];
if (red == green && red == blue)
{
[[background_color colorUsingColorSpaceName: NSCalibratedWhiteColorSpace]
getWhite: &gray alpha: &alpha];
snprintf (buf, sizeof (buf), "#%2.2lx%2.2lx%2.2lx",
lrint (gray * 0xff), lrint (gray * 0xff), lrint (gray * 0xff));
color_vector = build_string (buf);
goto done;
}
snprintf (buf, sizeof (buf), "#%2.2lx%2.2lx%2.2lx",
lrint (red*0xff), lrint (green*0xff), lrint (blue*0xff));
color_vector = build_string (buf);
done:
NSTRACE_MSG ("BACKGROUND COLOR: %s", buf);
Thanks,
Keith
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2017-08-22 17:04 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-21 20:51 How to use a float value with either GLYPH_DEBUG or NSTRACE Keith David Bershatsky
2017-08-21 21:30 ` Noam Postavsky
2017-08-21 23:55 ` Paul Eggert
-- strict thread matches above, loose matches on Subject: below --
2017-08-22 17:04 Keith David Bershatsky
2017-08-22 4:30 Keith David Bershatsky
2017-08-22 8:23 ` Anders Lindgren
2017-08-22 11:32 ` Noam Postavsky
2017-08-22 11:51 ` Anders Lindgren
2017-08-22 15:47 ` Paul Eggert
2017-08-21 21:45 Keith David Bershatsky
2017-08-21 17:07 Keith David Bershatsky
2017-08-21 20:07 ` Alan Third
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.