Skip to content

Commit

Permalink
MODIFIED: Make the Prolog flag float_overflow to affect read_term/3.
Browse files Browse the repository at this point in the history
With this flag setting, too large floats are read as infinity rather than
raising a syntax error.
  • Loading branch information
JanWielemaker committed Oct 11, 2023
1 parent 4768c8b commit ab41015
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
4 changes: 3 additions & 1 deletion man/overview.doc
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,9 @@ The smallest representable floating point number above 0.0. See also
\prologflagitem{float_overflow}{atom}{rw}
One of \const{error} (default) or \const{infinity}. The first is ISO
compliant. Using \const{infinity}, floating point overflow is mapped
to positive or negative \const{Inf}. See \secref{ieee-float}.
to positive or negative \const{Inf}. See \secref{ieee-float}. This
flag also affects read_term/3 and friends, causing them to read too
large floating point number as infinity.

\prologflagitem{float_rounding}{atom}{rw}
Defines how arithmetic rounds to a float. Defined values are
Expand Down
40 changes: 28 additions & 12 deletions src/Tests/core/test_read.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2011-2016, University of Amsterdam
Copyright (c) 2011-2023, University of Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -46,22 +47,23 @@
*/

test_read :-
run_tests([ read_term
]).
run_tests([ read_term,
read_numbers
]).

:- begin_tests(read_term).

test(singletons, Names == ['_a','_A','_0','A']) :-
term_string(_, "a(_,_a,_A,_0,A)",
[ singletons(Singletons)
]),
maplist(arg(1), Singletons, Names).
term_string(_, "a(_,_a,_A,_0,A)",
[ singletons(Singletons)
]),
maplist(arg(1), Singletons, Names).
test(warn_singletons, Messages =@= [singletons(a(_,_,_,_,_), ['_a','A'])]) :-
catch_messages(_,
term_string(_, "a(_,_a,_A,_0,A)",
[ singletons(warning)
]),
Messages).
catch_messages(_,
term_string(_, "a(_,_a,_A,_0,A)",
[ singletons(warning)
]),
Messages).
test(position,
[Query,TermPos,Comments] ==
[true,7-11,['$stream_position'(0,0,0,0)-"%hello"]]) :-
Expand Down Expand Up @@ -161,6 +163,20 @@

:- end_tests(read_term).

:- begin_tests(read_numbers).

test(float_overflow, error(syntax_error(float_overflow))) :-
term_string(_, '1.797693134862316e+308', []).
test(float_overflow, F =:= inf) :-
current_prolog_flag(float_overflow, Old),
setup_call_cleanup(
set_prolog_flag(float_overflow, infinity),
term_string(F, '1.797693134862316e+308', []),
set_prolog_flag(float_overflow, Old)).

:- end_tests(read_numbers).


%% catch_messages(+Kind, :Goal, -Messages) is semidet.

:- thread_local
Expand Down
12 changes: 10 additions & 2 deletions src/pl-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -2704,9 +2704,17 @@ ascii_to_double(cucharp s, cucharp e, double *dp)
errno = 0;
d = strtod((char*)s, &es);
if ( (cucharp)es == e || (e[0] == '.' && e+1 == (cucharp)es) )
{ if ( errno == ERANGE && fabs(d) > 1.0 )
return NUM_FOVERFLOW;
{ if ( errno == ERANGE )
{ GET_LD

if ( fabs(d) > 1.0 )
{ if ( !(LD->arith.f.flags & FLT_OVERFLOW) )
return NUM_FOVERFLOW;
} else
{ if ( !(LD->arith.f.flags & FLT_UNDERFLOW) )
return NUM_FOVERFLOW;
}
}
*dp = d;
return NUM_OK;
}
Expand Down

0 comments on commit ab41015

Please sign in to comment.