Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #66185 > unrolled thread
| Started by | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| First post | 2015-07-22 12:46 -0700 |
| Last post | 2015-07-24 11:47 -0700 |
| Articles | 20 on this page of 31 — 7 participants |
Back to article view | Back to comp.lang.c
Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 12:46 -0700
Re: Floating point to string, and back via `%a'... James Kuyper <jameskuyper@verizon.net> - 2015-07-22 16:19 -0400
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 13:49 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:01 -0700
Re: Floating point to string, and back via `%a'... James Kuyper <jameskuyper@verizon.net> - 2015-07-22 17:18 -0400
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:22 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 13:40 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:01 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:12 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:20 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:24 -0700
Re: Floating point to string, and back via `%a'... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-22 21:30 +0000
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:39 -0700
Re: Floating point to string, and back via `%a'... Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-07-22 19:49 -0600
Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-22 22:28 +0100
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:34 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:58 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:43 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:45 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:51 -0700
Re: Floating point to string, and back via `%a'... Robert Wessel <robertwessel2@yahoo.com> - 2015-07-22 18:21 -0500
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 16:58 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 16:52 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 17:08 -0700
Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 00:01 +0100
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 19:40 -0700
Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 22:32 -0700
Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 11:07 +0100
Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 11:25 +0100
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-24 11:44 -0700
Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-24 11:47 -0700
Page 1 of 2 [1] 2 Next page →
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 12:46 -0700 |
| Subject | Floating point to string, and back via `%a'... |
| Message-ID | <moorva$su7$1@speranza.aioe.org> |
How in the heck can I use the `%a' format specifier in
conjunction with sprintf/sscanf so that I can convert a
floating point to a string, and back? I have some code,
but it does not work with `%a'. Damn!
Also, why does it seem to work for a %f %lf sprintf to sscanf pair?
_______________________________________________________________
#include <stdio.h>
int
main(void)
{
char buf[256] = { '\0' };
double no = 1.0123456789;
printf("no = %.10f\n\n", no);
/* try to use default precision `%a' with sprintf/sscanf */
{
sprintf(buf, "%a", no);
double nc = 0.0;
int status = sscanf(buf, "%a", &nc);
if (! status || status == EOF)
{
printf("sscanf `%%a' error!\n");
}
printf("(%%s):buf = %s\n", buf);
printf("(%%a):nc = %f\n\n", nc);
}
/* try to use custom precision `%.10a' with sprintf/sscanf */
{
sprintf(buf, "%.10a", no);
double nc = 0.0;
int status = sscanf(buf, "%.10a", &nc);
if (! status || status == EOF)
{
printf("sscanf `%%.10a' error!\n");
}
printf("(%%s):buf = %s\n", buf);
printf("(%%.10a):nc = %.10f\n\n", nc);
}
/* try to use default precision `%f' with sprintf/sscanf */
{
sprintf(buf, "%f", no);
double nc = 0.0;
int status = sscanf(buf, "%lf", &nc);
if (! status || status == EOF)
{
printf("sscanf %%lf error!\n");
}
printf("(%%s):buf = %s\n", buf);
printf("(%%f):nc = %f\n\n", nc);
}
/* try to use custom precision `%.10f' with sprintf/sscanf */
{
sprintf(buf, "%.10f", no);
double nc = 0.0;
int status = sscanf(buf, "%lf", &nc);
if (! status || status == EOF)
{
printf("sscanf %%lf error!\n");
}
printf("(%%s):buf = %s\n", buf);
printf("(%%.10f):nc = %.10f\n\n", nc);
}
return 0;
}
_______________________________________________________________
What am I doing wrong? How do I use `%a' properly!
;^O
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-22 16:19 -0400 |
| Message-ID | <55AFFAEC.9060104@verizon.net> |
| In reply to | #66185 |
On 07/22/2015 03:46 PM, Chris M. Thomasson wrote: > How in the heck can I use the `%a' format specifier in > conjunction with sprintf/sscanf so that I can convert a > floating point to a string, and back? I have some code, > but it does not work with `%a'. Damn! It would help if you identified how it failed to work. Which compiler are you using? What options did you select when compiling? Did compilation fail with error messages? If so, provide those messages. Did it not do what you expected it to do when you ran it? If so, tell us what it actually did. > Also, why does it seem to work for a %f %lf sprintf to sscanf pair? Because of the 'l'. ... > double nc = 0.0; > int status = sscanf(buf, "%a", &nc); "%a" is for float. Use "%la" for double (7.21.6.2p11). Similarly, you should be using "%lf", not "%f". This applies to all of your sscanf() calls. The 'l' is unnecessary for printf() and sprintf(), because floats are automatically promoted to double when the type of the corresponding argument of a function has not been declared in a function prototype. However, it is also permitted, to allow the same format string to be used for both sprintf() and sscanf(). ... > int status = sscanf(buf, "%.10a", &nc); sscanf() does not allow you to specify the precision, and '.' is not a permitted character in sscanf() directives.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 13:49 -0700 |
| Message-ID | <moovlp$56p$1@speranza.aioe.org> |
| In reply to | #66190 |
> "James Kuyper" wrote in message news:55AFFAEC.9060104@verizon.net...
> > On 07/22/2015 03:46 PM, Chris M. Thomasson wrote:
> > How in the heck can I use the `%a' format specifier in
> > conjunction with sprintf/sscanf so that I can convert a
> > floating point to a string, and back? I have some code,
> > but it does not work with `%a'. Damn!
> It would help if you identified how it failed to work. Which compiler
> are you using? What options did you select when compiling? Did
> compilation fail with error messages? If so, provide those messages. Did
> it not do what you expected it to do when you ran it? If so, tell us
> what it actually did.
I am using MSVC 12. Default for a new project. No error message from the
compiler. It failed to convert a string populated with sprintf and %a back
to the original float.
> > Also, why does it seem to work for a %f %lf sprintf to sscanf pair?
> Because of the 'l'.
…
I think I tried that and it still did not work with %a on MSVC. It seems to
work on GCC, but does not seems to get the precision correct:
I just quickly typed this in...
http://codepad.org/yO2UisHn
__________________________________________________
/* try to use default precision `%a' added `%la' with sprintf/sscanf */
{
sprintf(buf, "%.10la", no);
double nc = 0.0;
int status = sscanf(buf, "%la", &nc);
if (! status || status == EOF)
{
printf("sscanf `%%a' error!\n");
}
printf("(%%s):buf = %s\n", buf);
printf("(%%a):nc = %f\n\n", nc);
}
__________________________________________________
This gives:
__________________________________________________
(%s):buf = 0x1.0329161f1fp+0
(%a):nc = 1.012346
__________________________________________________
I am missing a lot of accuracy here from:
1.0123456789
;^O
How am I messing things up now damnit!?
;^O
> > double nc = 0.0;
> > int status = sscanf(buf, "%a", &nc);
> "%a" is for float. Use "%la" for double (7.21.6.2p11). Similarly, you
> should be using "%lf", not "%f". This applies to all of your sscanf()
> calls. The 'l' is unnecessary for printf() and sprintf(), because floats
> are automatically promoted to double when the type of the corresponding
> argument of a function has not been declared in a function prototype.
> However, it is also permitted, to allow the same format string to be
> used for both sprintf() and sscanf().
…
Thank you!
> > int status = sscanf(buf, "%.10a", &nc);
> sscanf() does not allow you to specify the precision, and '.' is not a
> permitted character in sscanf() directives.
DOH! Thanks again James!
;^o
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-22 14:01 -0700 |
| Message-ID | <lnoaj329fy.fsf@kst-u.example.com> |
| In reply to | #66196 |
"Chris M. Thomasson" <nospam@nospam.nospam> writes:
[snip]
> This gives:
> __________________________________________________
> (%s):buf = 0x1.0329161f1fp+0
> (%a):nc = 1.012346
> __________________________________________________
>
>
>
> I am missing a lot of accuracy here from:
> 1.0123456789
[...]
1.012346 is the output using the "%f" format, which only uses 6 or so
decimal digits by default. Try changing "%f" to "%.20f" (which will
print more precision than you really have, but it will give you a better
idea of what the value isZ).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-22 17:18 -0400 |
| Message-ID | <55B00899.4030801@verizon.net> |
| In reply to | #66196 |
On 07/22/2015 04:49 PM, Chris M. Thomasson wrote:
>> "James Kuyper" wrote in message news:55AFFAEC.9060104@verizon.net...
>
>>> On 07/22/2015 03:46 PM, Chris M. Thomasson wrote:
>>> How in the heck can I use the `%a' format specifier in
>>> conjunction with sprintf/sscanf so that I can convert a
>>> floating point to a string, and back? I have some code,
>>> but it does not work with `%a'. Damn!
>
>> It would help if you identified how it failed to work. Which compiler
>> are you using? What options did you select when compiling? Did
>> compilation fail with error messages? If so, provide those messages. Did
>> it not do what you expected it to do when you ran it? If so, tell us
>> what it actually did.
>
> I am using MSVC 12. Default for a new project. No error message from the
> compiler. It failed to convert a string populated with sprintf and %a back
> to the original float.
So, what did it produce instead? Give the actual output from the program.
>>> Also, why does it seem to work for a %f %lf sprintf to sscanf pair?
>
>> Because of the 'l'.
> …
>
> I think I tried that and it still did not work with %a on MSVC. It seems to
"%a" was added in C99. Does MSVC 12 claim C99 compatibility? I know that
MSVC has been extremely slow to adopt C99, but I've heard that recently
they've improved a bit. It's contrary to MS's business model to
encourage the writing of code that could be ported away from MS
platforms, so they have not made standard conformance a priority.
> work on GCC, but does not seems to get the precision correct:
>
> I just quickly typed this in...
>
> http://codepad.org/yO2UisHn
>
> __________________________________________________
> /* try to use default precision `%a' added `%la' with sprintf/sscanf */
> {
> sprintf(buf, "%.10la", no);
>
> double nc = 0.0;
> int status = sscanf(buf, "%la", &nc);
>
> if (! status || status == EOF)
> {
> printf("sscanf `%%a' error!\n");
> }
>
> printf("(%%s):buf = %s\n", buf);
> printf("(%%a):nc = %f\n\n", nc);
> }
> __________________________________________________
>
>
> This gives:
> __________________________________________________
> (%s):buf = 0x1.0329161f1fp+0
> (%a):nc = 1.012346
> __________________________________________________
>
>
>
> I am missing a lot of accuracy here from:
> 1.0123456789
How can you tell? You didn't specify the precision in the %f format
string you used to print that value. The precision defaults to 6
(7.21.6.1p8), and 6 digits were printed out after the decimal point.
1.0123456789, rounded to 6 digits after the decimal point, is 1.012346,
so your output is perfectly consistent with the conversion having been
perfect. It could also be seriously imperfect - you didn't print enough
digits to tell.
Note: do NOT expect, if you do print enough digits, that you'll get
1.0123456789. That number cannot be represented exactly in any floating
point representation where FLT_RADIX is a power of 2, which is
overwhelming the most common case. The best you can hope for is that
"no" and "nc" represent the same exact value, one that is somewhat
different from 1.0123456789.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 14:22 -0700 |
| Message-ID | <mop1jb$9o6$1@speranza.aioe.org> |
| In reply to | #66200 |
> "James Kuyper" wrote in message news:55B00899.4030801@verizon.net... > > > On 07/22/2015 04:49 PM, Chris M. Thomasson wrote: > >> "James Kuyper" wrote in message news:55AFFAEC.9060104@verizon.net... > > >>> On 07/22/2015 03:46 PM, Chris M. Thomasson wrote: > >>> How in the heck can I use the `%a' format specifier in > >>> conjunction with sprintf/sscanf so that I can convert a > >>> floating point to a string, and back? I have some code, > >>> but it does not work with `%a'. Damn! > > > >> It would help if you identified how it failed to work. Which compiler > >> are you using? What options did you select when compiling? Did > >> compilation fail with error messages? If so, provide those messages. > >> Did > >> it not do what you expected it to do when you ran it? If so, tell us > >> what it actually did. > > > > I am using MSVC 12. Default for a new project. No error message from the > > compiler. It failed to convert a string populated with sprintf and %a > > back > > to the original float. > So, what did it produce instead? Give the actual output from the program. MSVC 12 always gives a damn zero! 0.0 Damn!
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-22 13:40 -0700 |
| Message-ID | <lntwsw0vur.fsf@kst-u.example.com> |
| In reply to | #66185 |
"Chris M. Thomasson" <nospam@nospam.nospam> writes:
> How in the heck can I use the `%a' format specifier in
> conjunction with sprintf/sscanf so that I can convert a
> floating point to a string, and back? I have some code,
> but it does not work with `%a'. Damn!
>
> Also, why does it seem to work for a %f %lf sprintf to sscanf pair?
> _______________________________________________________________
> #include <stdio.h>
>
>
> int
> main(void)
> {
> char buf[256] = { '\0' };
> double no = 1.0123456789;
>
> printf("no = %.10f\n\n", no);
>
>
> /* try to use default precision `%a' with sprintf/sscanf */
> {
> sprintf(buf, "%a", no);
>
> double nc = 0.0;
> int status = sscanf(buf, "%a", &nc);
>
> if (! status || status == EOF)
> {
> printf("sscanf `%%a' error!\n");
> }
>
> printf("(%%s):buf = %s\n", buf);
> printf("(%%a):nc = %f\n\n", nc);
> }
[SNIP]
> What am I doing wrong? How do I use `%a' properly!
It's an annoying but unavoidable asymmetry between printf and scanf.
For printf, the "%a" format requires a double argument. There's no
disinct format for float, because float arguments are promoted to double
(because printf is a variadic function).
But scanf takes pointers as arguments, and you can't meaningfully
convert a float* to a double* or vice versa. So for scanf (or sscanf),
"%a" requires a float* argument, while "%la" requires a double*
argument. (And "%La" requires a long double* argument).
Change "%a" to "%la" in your sscanf call (and leave it as "%a" in your
printf call).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 14:01 -0700 |
| Message-ID | <mop0bg$6r8$1@speranza.aioe.org> |
| In reply to | #66192 |
> "Keith Thompson" wrote in message news:lntwsw0vur.fsf@kst-u.example.com...
> > "Chris M. Thomasson" <nospam@nospam.nospam> writes:
> > How in the heck can I use the `%a' format specifier in
> > conjunction with sprintf/sscanf so that I can convert a
> > floating point to a string, and back? I have some code,
> > but it does not work with `%a'. Damn!
[...]
> > http://codepad.org/yO2UisHn
> Change "%a" to "%la" in your sscanf call (and leave it as "%a" in your
> printf call).
I am now thinking about something simple that "should work" for
my application as long as I stick to a std representation of
floating point numbers.
http://codepad.org/UxAQn9oi
#include <stdio.h>
_______________________________________________________________
int
main(void)
{
char buf[256] = { '\0' };
double no = 1.0123456789;
printf("no = %.10f\n\n", no);
/* try to use custom precision `%.10lf' with sprintf/sscanf */
{
sprintf(buf, "%.10lf", no);
double nc = 0.0;
int status = sscanf(buf, "%lf", &nc);
if (! status || status == EOF)
{
printf("sscanf %%lf error!\n");
}
printf("(%%s):buf = %s\n", buf);
printf("(%%f):nc = %f\n\n", nc);
printf("Hey man, why not simply use %.10lf as the public key?
:^D\n\n", nc);
}
return 0;
}
_______________________________________________________________
What do you think Keith?
Even if the precision of the public key is beyond %.10, well, it
does not matter because I can use it after the "rounding"
(double=>string=>double). I will be sending the %.10 across the
wire anyway. Humm...
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-22 14:12 -0700 |
| Message-ID | <lnk2tr28x9.fsf@kst-u.example.com> |
| In reply to | #66198 |
"Chris M. Thomasson" <nospam@nospam.nospam> writes:
>> "Keith Thompson" wrote in message news:lntwsw0vur.fsf@kst-u.example.com...
>
>> > "Chris M. Thomasson" <nospam@nospam.nospam> writes:
>> > How in the heck can I use the `%a' format specifier in
>> > conjunction with sprintf/sscanf so that I can convert a
>> > floating point to a string, and back? I have some code,
>> > but it does not work with `%a'. Damn!
> [...]
>> > http://codepad.org/yO2UisHn
>
>> Change "%a" to "%la" in your sscanf call (and leave it as "%a" in your
>> printf call).
>
> I am now thinking about something simple that "should work" for
> my application as long as I stick to a std representation of
> floating point numbers.
>
> http://codepad.org/UxAQn9oi
>
> #include <stdio.h>
>
> _______________________________________________________________
> int
> main(void)
> {
> char buf[256] = { '\0' };
> double no = 1.0123456789;
>
> printf("no = %.10f\n\n", no);
>
> /* try to use custom precision `%.10lf' with sprintf/sscanf */
> {
> sprintf(buf, "%.10lf", no);
>
> double nc = 0.0;
> int status = sscanf(buf, "%lf", &nc);
>
> if (! status || status == EOF)
> {
> printf("sscanf %%lf error!\n");
> }
>
> printf("(%%s):buf = %s\n", buf);
> printf("(%%f):nc = %f\n\n", nc);
>
> printf("Hey man, why not simply use %.10lf as the public key?
> :^D\n\n", nc);
> }
>
> return 0;
> }
> _______________________________________________________________
>
>
> What do you think Keith?
>
> Even if the precision of the public key is beyond %.10, well, it
> does not matter because I can use it after the "rounding"
> (double=>string=>double). I will be sending the %.10 across the
> wire anyway. Humm...
"%a" (with appropriate length modifiers) should give you an exact
representation of any floating-point value, as long as FLT_RADIX is a
power of 2.
What do you mean by "public key"? That term is mostly used in
cryptography. Public keys are generally binary values that can be
represented as very long integers or as a sequence of hex digits. Using
floating-point for public keys makes little or no sense.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 14:20 -0700 |
| Message-ID | <mop1f6$9dv$1@speranza.aioe.org> |
| In reply to | #66199 |
> "Keith Thompson" wrote in message > news:lnk2tr28x9.fsf@kst-u.example.com... > > "Chris M. Thomasson" <nospam@nospam.nospam> writes: [...] > > What do you think Keith? > > > > Even if the precision of the public key is beyond %.10, well, it > > does not matter because I can use it after the "rounding" > > (double=>string=>double). I will be sending the %.10 across the > > wire anyway. Humm... > "%a" (with appropriate length modifiers) should give you an exact > representation of any floating-point value, as long as FLT_RADIX is a > power of 2. > What do you mean by "public key"? That term is mostly used in > cryptography. Public keys are generally binary values that can be > represented as very long integers or as a sequence of hex digits. Using > floating-point for public keys makes little or no sense. The public key is comprised of 4 floating point numbers that define a region of the complex plane. This region is then used to generate a fractal based off of a private fractal comprised of a Julia set, iteration count, location, and some color pallet settings. The formulas and keys are used for the cipher: https://groups.google.com/d/topic/sci.crypt/g6CPhD9mh24/discussion The public key is sent across the wire as a location into the private key. The keys are fractal formulas and locations, so they tend to have a lot of individual space to play around with... The toy I created seems kind of fun to play with, IMHO at least. ;^) See if it works for you? Can you get it to compile? ;^o
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 14:24 -0700 |
| Message-ID | <mop1mo$9ta$1@speranza.aioe.org> |
| In reply to | #66201 |
> "Chris M. Thomasson" wrote in message > news:mop1f6$9dv$1@speranza.aioe.org... [...] > The public key is comprised of 4 floating point numbers that > define a region of the complex plane. This region is then > used to generate a fractal based off of a private fractal > comprised of a Julia set, iteration count, location, and some > color pallet settings. The formulas and keys are used for the > cipher: [...] I also need to add a function parser because the private key should have a user-defined formula. Not sure if I want the public keys to have their own formulas... Yikes!
[toc] | [prev] | [next] | [standalone]
| From | glen herrmannsfeldt <gah@ugcs.caltech.edu> |
|---|---|
| Date | 2015-07-22 21:30 +0000 |
| Message-ID | <mop216$a3o$1@speranza.aioe.org> |
| In reply to | #66201 |
Chris M. Thomasson <nospam@nospam.nospam> wrote: >> "Keith Thompson" wrote in message (snip) >> What do you mean by "public key"? That term is mostly used in >> cryptography. Public keys are generally binary values that can be >> represented as very long integers or as a sequence of hex digits. Using >> floating-point for public keys makes little or no sense. > The public key is comprised of 4 floating point numbers that > define a region of the complex plane. This region is then > used to generate a fractal based off of a private fractal > comprised of a Julia set, iteration count, location, and some > color pallet settings. The formulas and keys are used for the > cipher: > https://groups.google.com/d/topic/sci.crypt/g6CPhD9mh24/discussion Public keys should be fixed point numbers, even if you use floating point to calculate them. (But it is better to do the calculation in fixed point.) -- glen
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 14:39 -0700 |
| Message-ID | <mop2i7$bkq$2@speranza.aioe.org> |
| In reply to | #66206 |
> "glen herrmannsfeldt" wrote in message > news:mop216$a3o$1@speranza.aioe.org... > > Chris M. Thomasson <nospam@nospam.nospam> wrote: [...] > Public keys should be fixed point numbers, even if you use > floating point to calculate them. (But it is better to do > the calculation in fixed point.) I always have the option to send integer pairs to represent floating points: https://groups.google.com/d/topic/sci.math/5YXiqfkjrF4/discussion I have a function that gains a close approximation using convergents generated from a continued fraction iteration. This does seem cleaner, and smaller wrt sending data! Two integers vs a single double. ;^) Humm... the pubic key can be comprised out of integer pairs that get converted to an agreed upon std floating point representation on the receiving end. I just might use this. Humm...
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2015-07-22 19:49 -0600 |
| Message-ID | <1bvbdbr6cb.fsf@pfeifferfamily.net> |
| In reply to | #66201 |
"Chris M. Thomasson" <nospam@nospam.nospam> writes: >> "Keith Thompson" wrote in message >> news:lnk2tr28x9.fsf@kst-u.example.com... > >> > "Chris M. Thomasson" <nospam@nospam.nospam> writes: > [...] > >> > What do you think Keith? >> > >> > Even if the precision of the public key is beyond %.10, well, it >> > does not matter because I can use it after the "rounding" >> > (double=>string=>double). I will be sending the %.10 across the >> > wire anyway. Humm... > >> "%a" (with appropriate length modifiers) should give you an exact >> representation of any floating-point value, as long as FLT_RADIX is a >> power of 2. > > >> What do you mean by "public key"? That term is mostly used in >> cryptography. Public keys are generally binary values that can be >> represented as very long integers or as a sequence of hex digits. Using >> floating-point for public keys makes little or no sense. > > The public key is comprised of 4 floating point numbers that > define a region of the complex plane. This region is then > used to generate a fractal based off of a private fractal > comprised of a Julia set, iteration count, location, and some > color pallet settings. The formulas and keys are used for the > cipher: > > https://groups.google.com/d/topic/sci.crypt/g6CPhD9mh24/discussion Using a floating point number in cryptography seems like it's really fraught with peril. The prospect of differing precisions, or even errors in rounding algorithms on something like this seems like it's just asking for bugs, if not today then a decade down the line when a processor implementation changes. I can imagine using a private floating point representation using a custom floating point arithmetic library for something like this.... barely....
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-07-22 22:28 +0100 |
| Message-ID | <87mvyn51bm.fsf@bsb.me.uk> |
| In reply to | #66185 |
"Chris M. Thomasson" <nospam@nospam.nospam> writes: > How in the heck can I use the `%a' format specifier in conjunction > with sprintf/sscanf so that I can convert a floating point to a > string, and back? I have some code, but it does not work with > `%a'. Damn! Judging by your posts in sci.crypt, the problem is the default precision use by MSVC's C library. The MSVC 2015 documentation says the default precision is 6 digits, but the C standard mandate that it be enough to represent the number (or at least to distinguish all numerical values): 7.19.6.1 p8: "... if the precision is missing and FLT_RADIX is a power of 2, then the precision is sufficient for an exact representation of the value; if the precision is missing and FLT_RADIX is not a power of 2, then the precision is sufficient to distinguish 245) values of type double, ..." It looks like MSVC simply has a bug. You could calculate the number of digits you need but that would be fiddly to get right in every single case. <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 14:34 -0700 |
| Message-ID | <mop2i6$bkq$1@speranza.aioe.org> |
| In reply to | #66205 |
> "Ben Bacarisse" wrote in message news:87mvyn51bm.fsf@bsb.me.uk... > > "Chris M. Thomasson" <nospam@nospam.nospam> writes: > > How in the heck can I use the `%a' format specifier in conjunction > > with sprintf/sscanf so that I can convert a floating point to a > > string, and back? I have some code, but it does not work with > > `%a'. Damn! > Judging by your posts in sci.crypt, the problem is the default precision > use by MSVC's C library. The MSVC 2015 documentation says the default > precision is 6 digits, but the C standard mandate that it be enough to > represent the number (or at least to distinguish all numerical values): > 7.19.6.1 p8: > "... if the precision is missing and FLT_RADIX is a power of 2, then > the precision is sufficient for an exact representation of the value; > if the precision is missing and FLT_RADIX is not a power of 2, then > the precision is sufficient to distinguish 245) values of type double, > ..." > It looks like MSVC simply has a bug. You could calculate the number of > digits you need but that would be fiddly to get right in every single > case. Right. FWIW, %a does not work on MSVC for sscanf always returns zero. So, I am going to use %lf and stick to a precision of %.10 for now. I do not care if the double to string back to double conversion looses precision because the fractal can be generated on the end result of the conversion, not the original double. So, I get some doubles, convert then to strings and back, then used the new results regards of any lost precision. Yup. This should work...
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-22 14:58 -0700 |
| Message-ID | <lnfv4f26t4.fsf@kst-u.example.com> |
| In reply to | #66208 |
"Chris M. Thomasson" <nospam@nospam.nospam> writes:
[...]
>> It looks like MSVC simply has a bug. You could calculate the number of
>> digits you need but that would be fiddly to get right in every single
>> case.
>
> Right. FWIW, %a does not work on MSVC for sscanf always returns zero.
Does MSVC just not implement %a? Did you try changing to %la (you may
have already answered that).
Try this test program:
#include <stdio.h>
int main(void) {
char buf[256];
double in = 1.0123456789;
double out = 1.0123456789;
int result;
sprintf(buf, "%a", in);
result = sscanf(buf, "%la", &out);
printf("in = %.10f = %a\n", in, in);
printf("buf = \"%s\"\n", buf);
printf("sscanf returned %d\n", result);
printf("out = %.10f = %a\n", out, out);
if (in == out) {
puts("in == out");
}
else {
puts("in != out");
}
return 0;
}
On my system, the output is:
in = 1.0123456789 = 0x1.0329161f1f56cp+0
buf = "0x1.0329161f1f56cp+0"
sscanf returned 1
out = 1.0123456789 = 0x1.0329161f1f56cp+0
in == out
What output do you get using MSVC? If it's significantly different,
there may well be a bug in MSVC (presumably in its runtime library).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 15:43 -0700 |
| Message-ID | <mop6bc$j5h$1@speranza.aioe.org> |
| In reply to | #66210 |
> "Keith Thompson" wrote in message > news:lnfv4f26t4.fsf@kst-u.example.com... > > "Chris M. Thomasson" <nospam@nospam.nospam> writes: [...] > >> It looks like MSVC simply has a bug. You could calculate the number of > >> digits you need but that would be fiddly to get right in every single > >> case. > > > > Right. FWIW, %a does not work on MSVC for sscanf always returns zero. > Does MSVC just not implement %a? Did you try changing to %la (you may > have already answered that). > Try this test program: [...] > On my system, the output is: > in = 1.0123456789 = 0x1.0329161f1f56cp+0 > buf = "0x1.0329161f1f56cp+0" > sscanf returned 1 > out = 1.0123456789 = 0x1.0329161f1f56cp+0 > in == out > What output do you get using MSVC? If it's significantly different, > there may well be a bug in MSVC (presumably in its runtime library). I am getting: ________________________________________ in = 1.0123456789 = 0x1.032916p+0 buf = "0x1.032916p+0" sscanf returned 0 out = 1.0123456789 = 0x1.032916p+0 in == out ________________________________________ If I change out to 0.0, I get: ________________________________________ in = 1.0123456789 = 0x1.032916p+0 buf = "0x1.032916p+0" sscanf returned 0 out = 0.0000000000 = 0x0.000000p+0 in != out ________________________________________ sscanf is failing over hear on MSVC 12. DAMN!
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 15:45 -0700 |
| Message-ID | <mop6fc$jeb$1@speranza.aioe.org> |
| In reply to | #66210 |
> "Keith Thompson" wrote in message > news:lnfv4f26t4.fsf@kst-u.example.com... > > "Chris M. Thomasson" <nospam@nospam.nospam> writes: [...] > >> It looks like MSVC simply has a bug. You could calculate the number of > >> digits you need but that would be fiddly to get right in every single > >> case. > > > > Right. FWIW, %a does not work on MSVC for sscanf always returns zero. > Does MSVC just not implement %a? Did you try changing to %la (you may > have already answered that). > Try this test program: FWIW, I get: ______________________________________ in = 1.0123456789 = 0x1.0329161f1f56cp+0 buf = "0x1.0329161f1f56cp+0" sscanf returned 1 out = 1.0123456789 = 0x1.0329161f1f56cp+0 in == out ______________________________________ on codepad: http://codepad.org/FXS1qYG1
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-07-22 15:51 -0700 |
| Message-ID | <mop6qm$juc$1@speranza.aioe.org> |
| In reply to | #66210 |
> "Keith Thompson" wrote in message > news:lnfv4f26t4.fsf@kst-u.example.com... [...] > What output do you get using MSVC? If it's significantly different, > there may well be a bug in MSVC (presumably in its runtime library). I get with with MING 4.8 on windows: _________________________________________ in = 1.0123456789 = 0x1.032916p+0 buf = "0x1.032916p+0" sscanf returned 0 out = 0.0000000000 = 0x0.000000p+0 in != out _________________________________________ So, the nasty problem has to be in ms runtime shi%. Damn! ;^(
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web